Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5355 > unrolled thread

Converting a set into list

Started byTheSaint <nobody@nowhere.net.no>
First post2011-05-14 17:02 +0800
Last post2011-05-17 01:07 +0000
Articles 20 on this page of 22 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-14 17:02 +0800
    Re: Converting a set into list Peter Otten <__peter__@web.de> - 2011-05-14 11:33 +0200
      Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-14 22:14 +0800
        Re: Converting a set into list Chris Angelico <rosuav@gmail.com> - 2011-05-15 04:22 +1000
    Re: Converting a set into list Ben Finney <ben+python@benfinney.id.au> - 2011-05-15 00:12 +1000
      Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-14 22:51 +0800
        Re: Converting a set into list Ben Finney <ben+python@benfinney.id.au> - 2011-05-15 09:21 +1000
          Re: Converting a set into list Chris Torek <nospam@torek.net> - 2011-05-15 02:11 +0000
            Re: Converting a set into list SigmundV <sigmundv@gmail.com> - 2011-05-15 04:18 -0700
              Re: Converting a set into list SigmundV <sigmundv@gmail.com> - 2011-05-15 04:23 -0700
              Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-15 23:56 +0800
                Re: Converting a set into list Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-05-15 22:27 +0200
                  Re: Converting a set into list Daniel Kluev <dan.kluev@gmail.com> - 2011-05-16 13:37 +1100
                  Re: Converting a set into list Peter Otten <__peter__@web.de> - 2011-05-16 08:34 +0200
                  Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-16 22:23 +0800
                    Re: Converting a set into list Ben Finney <ben+python@benfinney.id.au> - 2011-05-17 10:33 +1000
              Re: Converting a set into list Roy Smith <roy@panix.com> - 2011-05-15 13:07 -0400
            Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-16 00:05 +0800
              Re: Converting a set into list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-15 16:28 +0000
                Re: Converting a set into list TheSaint <nobody@nowhere.net.no> - 2011-05-16 00:35 +0800
            Re: Converting a set into list Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-16 10:24 +0000
              Re: Converting a set into list Chris Torek <nospam@torek.net> - 2011-05-17 01:07 +0000

Page 1 of 2  [1] 2  Next page →


#5355 — Converting a set into list

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-14 17:02 +0800
SubjectConverting a set into list
Message-ID<iqlgj4$iet$1@speranza.aioe.org>
Hello

I've stumble to find a solution to get a list from a set

<code>

>>> aa= ['a','b','c','f']
>>> aa
['a', 'b', 'c', 'f']
>>> set(aa)
{'a', 'c', 'b', 'f'}
>>> [k for k in aa]
['a', 'b', 'c', 'f']

</code>
I repute the comprehension list too expensive, is there another method?

-- 
goto /dev/null

[toc] | [next] | [standalone]


#5358

FromPeter Otten <__peter__@web.de>
Date2011-05-14 11:33 +0200
Message-ID<mailman.1543.1305365545.9059.python-list@python.org>
In reply to#5355
TheSaint wrote:

> I've stumble to find a solution to get a list from a set
> 
> <code>
> 
>>>> aa= ['a','b','c','f']
>>>> aa
> ['a', 'b', 'c', 'f']
>>>> set(aa)

To clarify: this creates a new object, so aa is still a list.

> {'a', 'c', 'b', 'f'}
>>>> [k for k in aa]
> ['a', 'b', 'c', 'f']

So you are actually converting a list to a (new) list here. Of course it 
would have worked with a set or an arbitrary iterable, too.

> </code>
> I repute the comprehension list too expensive, is there another method?
 
mylist = list(myset)

Do you notice the similarity to converting a list to a set?

[toc] | [prev] | [next] | [standalone]


#5366

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-14 22:14 +0800
Message-ID<iqm2oi$u60$1@speranza.aioe.org>
In reply to#5358
Peter Otten wrote:

> mylist = list(myset)
> Do you notice the similarity to converting a list to a set?
> 
There was something confusing me yesterday in doing that, but (for me 
strangely) I got cleared out.

The point was that after a result from:

newset= set(myset1) & set(myset2)
list= [newset]

<< [{'bla', 'alb', 'lab'}]

Probably list(set) is not like [set].

-- 
goto /dev/null

[toc] | [prev] | [next] | [standalone]


#5376

FromChris Angelico <rosuav@gmail.com>
Date2011-05-15 04:22 +1000
Message-ID<mailman.1553.1305397356.9059.python-list@python.org>
In reply to#5366
On Sun, May 15, 2011 at 12:14 AM, TheSaint <nobody@nowhere.net.no> wrote:
> newset= set(myset1) & set(myset2)
> list= [newset]
>
> << [{'bla', 'alb', 'lab'}]
>
> Probably list(set) is not like [set].

list(set) creates a list out of the set. [set] creates a list with one
element, the set itself. It's not a copy of the set, it's another
reference to the same set; change one and you'll see the change in the
other.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#5365

FromBen Finney <ben+python@benfinney.id.au>
Date2011-05-15 00:12 +1000
Message-ID<87iptdid68.fsf@benfinney.id.au>
In reply to#5355
TheSaint <nobody@nowhere.net.no> writes:

> Hello
>
> I've stumble to find a solution to get a list from a set
>
> <code>
>
> >>> aa= ['a','b','c','f']

Creates a new list object. Binds the name ‘aa’ to that object.

> >>> aa
> ['a', 'b', 'c', 'f']

Evaluates the object referenced by the name ‘aa’.

> >>> set(aa)
> {'a', 'c', 'b', 'f'}

Creates a new set object, populating it with the contents from the list
object referenced by ‘aa’. Doesn't do anything with the new set object,
which will soon be garbage-collected.

> >>> [k for k in aa]
> ['a', 'b', 'c', 'f']

Creates a new list object by iterating each of the items from the list
referenced by ‘aa’. Does nothing with the new list object, which will
soon be garbage-collected.

> </code>
> I repute the comprehension list too expensive, is there another method?

Another method to do what?

If you want to bind ‘aa’ to a new object, do so with an assignment
statement. (Your example has exactly one assignment statement; all the
other statements create objects which are never bound to anything.)

But what is it you actually want to do?

-- 
 \          “The fact that I have no remedy for all the sorrows of the |
  `\     world is no reason for my accepting yours. It simply supports |
_o__)  the strong probability that yours is a fake.” —Henry L. Mencken |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#5368

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-14 22:51 +0800
Message-ID<iqm4tk$3sl$1@speranza.aioe.org>
In reply to#5365
Ben Finney wrote:

> Another method to do what?
> 
Sorry, some time we expect to have said it as we thought it.

The example was to show that after having made a set

set(aa)

the need to get that set converted into a list.
My knowledge drove me to use a comprehension list as a converter.
In another post I got to know the simplest way to state

list(aa)
Where aa is a set.

-- 
goto /dev/null

[toc] | [prev] | [next] | [standalone]


#5391

FromBen Finney <ben+python@benfinney.id.au>
Date2011-05-15 09:21 +1000
Message-ID<871v00j2bh.fsf@benfinney.id.au>
In reply to#5368
TheSaint <nobody@nowhere.net.no> writes:

> The example was to show that after having made a set
>
> set(aa)
>
> the need to get that set converted into a list.

As pointed out: you already know how to create a set from an object;
creating a list from an object is very similar:

    list(set(aa))

But why are you doing that? What are you trying to achieve?

-- 
 \     “We are all agreed that your theory is crazy. The question that |
  `\      divides us is whether it is crazy enough to have a chance of |
_o__)            being correct.” —Niels Bohr (to Wolfgang Pauli), 1958 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#5403

FromChris Torek <nospam@torek.net>
Date2011-05-15 02:11 +0000
Message-ID<iqncnv01u6u@news5.newsguy.com>
In reply to#5391
In article <871v00j2bh.fsf@benfinney.id.au>
Ben Finney  <ben+python@benfinney.id.au> wrote:
>As pointed out: you already know how to create a set from an object;
>creating a list from an object is very similar:
>
>    list(set(aa))
>
>But why are you doing that? What are you trying to achieve?

I have no idea why someone *else* is doing that, but I have used
this very expression to unique-ize a list:

    >>> x = [3, 1, 4, 1, 5, 9, 2, 6]
    >>> x
    [3, 1, 4, 1, 5, 9, 2, 6]
    >>> list(set(x))
    [1, 2, 3, 4, 5, 6, 9]
    >>>

Of course, this trick only works if all the list elements are
hashable.

This might not be the best example since the result is sorted
"by accident", while other list(set(...)) results are not.  Add
sorted() or .sort() if needed:

    >>> x = ['three', 'one', 'four', 'one', 'five']
    >>> x
    ['three', 'one', 'four', 'one', 'five']
    >>> list(set(x))
    ['four', 'five', 'three', 'one']
    >>> sorted(list(set(x)))
    ['five', 'four', 'one', 'three']
    >>> 
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

[toc] | [prev] | [next] | [standalone]


#5423

FromSigmundV <sigmundv@gmail.com>
Date2011-05-15 04:18 -0700
Message-ID<34fc571c-f382-405d-94b1-0a673da5f46b@t16g2000vbi.googlegroups.com>
In reply to#5403
I think the OP wants to find the intersection of two lists.
list(set(list1) & set(list2)) is indeed one way to achieve this. [i
for i in list1 if i in list2] is another one.

Sigmund

On May 15, 4:11 am, Chris Torek <nos...@torek.net> wrote:
> In article <871v00j2bh....@benfinney.id.au>
> Ben Finney  <ben+pyt...@benfinney.id.au> wrote:
>
> >As pointed out: you already know how to create a set from an object;
> >creating a list from an object is very similar:
>
> >    list(set(aa))
>
> >But why are you doing that? What are you trying to achieve?
>
> I have no idea why someone *else* is doing that, but I have used
> this very expression to unique-ize a list:
>
>     >>> x = [3, 1, 4, 1, 5, 9, 2, 6]
>     >>> x
>     [3, 1, 4, 1, 5, 9, 2, 6]
>     >>> list(set(x))
>     [1, 2, 3, 4, 5, 6, 9]
>     >>>
>
> Of course, this trick only works if all the list elements are
> hashable.
>
> This might not be the best example since the result is sorted
> "by accident", while other list(set(...)) results are not.  Add
> sorted() or .sort() if needed:
>
>     >>> x = ['three', 'one', 'four', 'one', 'five']
>     >>> x
>     ['three', 'one', 'four', 'one', 'five']
>     >>> list(set(x))
>     ['four', 'five', 'three', 'one']
>     >>> sorted(list(set(x)))
>     ['five', 'four', 'one', 'three']
>     >>>
> --
> In-Real-Life: Chris Torek, Wind River Systems
> Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
> email: gmail (figure it out)      http://web.torek.net/torek/index.html

[toc] | [prev] | [next] | [standalone]


#5424

FromSigmundV <sigmundv@gmail.com>
Date2011-05-15 04:23 -0700
Message-ID<7b1ee48b-cd41-4baa-bfa2-473e7d898150@gu8g2000vbb.googlegroups.com>
In reply to#5423
I'm sorry I top posted. I'll remember not to top post next time.

Sigmund

[toc] | [prev] | [next] | [standalone]


#5430

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-15 23:56 +0800
Message-ID<iqot4d$cd6$1@speranza.aioe.org>
In reply to#5423
SigmundV wrote:

> I think the OP wants to find the intersection of two lists.
> list(set(list1) & set(list2)) is indeed one way to achieve this. [i
> for i in list1 if i in list2] is another one

Exactly. I was confused on that I wasn't able to have a list in return.
The set intersection is the smartest result better than a "for" loop or a 
comprehension list.
Infact the operatin loops are compiled into python, therfore they are the 
fastest.
-- 
goto /dev/null

[toc] | [prev] | [next] | [standalone]


#5444

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-05-15 22:27 +0200
Message-ID<iqpd07$3gs$1@r03.glglgl.eu>
In reply to#5430
Am 15.05.2011 17:56 schrieb TheSaint:
> SigmundV wrote:
>
>> I think the OP wants to find the intersection of two lists.
>> list(set(list1)&  set(list2)) is indeed one way to achieve this. [i
>> for i in list1 if i in list2] is another one
>
> Exactly. I was confused on that I wasn't able to have a list in return.
> The set intersection is the smartest result better than a "for" loop or a
> comprehension list.

I'm not sure about if it is really the smartest way.

s=set(list2);  [i for i in list1 if i in s]
is in the same order of magnitude as the set operation. Both solutions 
seem to be equivalent in that concerns the number of needed loop runs, 
but this two-step operation might require one less loop over list1.

The set&set solution, in contrary, might require one loop while 
transforming to a set and another one for the & operation.


> Infact the operatin loops are compiled into python, therfore they are the
> fastest.

Which loops do you mean here?


Thomas

[toc] | [prev] | [next] | [standalone]


#5465

FromDaniel Kluev <dan.kluev@gmail.com>
Date2011-05-16 13:37 +1100
Message-ID<mailman.1612.1305513440.9059.python-list@python.org>
In reply to#5444
> Both solutions seem to be equivalent in that concerns the number of needed loop runs, but this two-step operation might require one less loop over list1.
> The set&set solution, in contrary, might require one loop while transforming to a set and another one for the & operation.

python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)"
"l3 = list(set(l1) & set(l2))"
100 loops, best of 3: 2.19 msec per loop

python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)"
"s=set(l2); l3 = [i for i in l1 if i in s]"
100 loops, best of 3: 2.45 msec per loop

python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)"
"l3 = list(set(l1) & set(l2))"
10 loops, best of 3: 28 msec per loop

python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)"
"s=set(l2); l3 = [i for i in l1 if i in s]"
10 loops, best of 3: 28.1 msec per loop

So even with conversion back into list set&set is still marginally faster.

-- 
With best regards,
Daniel Kluev

[toc] | [prev] | [next] | [standalone]


#5493

FromPeter Otten <__peter__@web.de>
Date2011-05-16 08:34 +0200
Message-ID<mailman.1629.1305527630.9059.python-list@python.org>
In reply to#5444
Daniel Kluev wrote:

>> Both solutions seem to be equivalent in that concerns the number of
>> needed loop runs, but this two-step operation might require one less loop
>> over list1. The set&set solution, in contrary, might require one loop
>> while transforming to a set and another one for the & operation.
> 
> python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)"
> "l3 = list(set(l1) & set(l2))"
> 100 loops, best of 3: 2.19 msec per loop
> 
> python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)"
> "s=set(l2); l3 = [i for i in l1 if i in s]"
> 100 loops, best of 3: 2.45 msec per loop
> 
> python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)"
> "l3 = list(set(l1) & set(l2))"
> 10 loops, best of 3: 28 msec per loop
> 
> python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)"
> "s=set(l2); l3 = [i for i in l1 if i in s]"
> 10 loops, best of 3: 28.1 msec per loop
> 
> So even with conversion back into list set&set is still marginally faster.

If you are looking for speed, consider

s = set(l1)
s.intersection_update(l2)
l3 = list(s) 

$ python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)" 
"list(set(l1) & set(l2))"
100 loops, best of 3: 4 msec per loop

$ python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)" "s = 
set(l1); s.intersection_update(l2); list(s)"
100 loops, best of 3: 1.99 msec per loop

[toc] | [prev] | [next] | [standalone]


#5520

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-16 22:23 +0800
Message-ID<iqrc0f$7e8$1@speranza.aioe.org>
In reply to#5444
Thomas Rachel wrote:

> Which loops do you mean here?

list(set) has been proved to largely win against
list = []
for item in set:
    list.append(item)
or [list.append(item) for item in set]

-- 
goto /dev/null

[toc] | [prev] | [next] | [standalone]


#5542

FromBen Finney <ben+python@benfinney.id.au>
Date2011-05-17 10:33 +1000
Message-ID<87mximf9o3.fsf@benfinney.id.au>
In reply to#5520
TheSaint <nobody@nowhere.net.no> writes:

> Thomas Rachel wrote:
>
> > Which loops do you mean here?
>
> list(set) has been proved to largely win against
> list = []
> for item in set:
>     list.append(item)
> or [list.append(item) for item in set]

Remember that the criterion of speed is a matter of the implementation,
and what's fast on one won't necessarily be fast on others. Which
implementations did you try?

Where I do agree is that ‘list(foo)’ wins over the other examples you
show on the important criteria of concision and readability.

-- 
 \          “A thing moderately good is not so good as it ought to be. |
  `\        Moderation in temper is always a virtue; but moderation in |
_o__)                       principle is always a vice.” —Thomas Paine |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#5439

FromRoy Smith <roy@panix.com>
Date2011-05-15 13:07 -0400
Message-ID<roy-B41F2E.13072415052011@news.panix.com>
In reply to#5423
In article 
<34fc571c-f382-405d-94b1-0a673da5f46b@t16g2000vbi.googlegroups.com>,
 SigmundV <sigmundv@gmail.com> wrote:

> I think the OP wants to find the intersection of two lists.
> list(set(list1) & set(list2)) is indeed one way to achieve this. [i
> for i in list1 if i in list2] is another one.

Both ways work, but the first is O(n) and the second is O(n^2).

import time
n = 10000
list1 = range(n)
list2 = range(n)
t0 = time.time()
list(set(list1) & set(list2))
t1 = time.time()
print "list(set) method took %f seconds" % (t1 - t0)
t0 = time.time()
[i for i in list1 if i in list2]
t1 = time.time()
print "loop method took %f seconds" % (t1 - t0)


./intersect.py 100000
list(set) method took 0.004791 seconds
loop method took 1.437322 seconds

[toc] | [prev] | [next] | [standalone]


#5431

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-16 00:05 +0800
Message-ID<iqotko$cd6$2@speranza.aioe.org>
In reply to#5403
Chris Torek wrote:

> >>> x = ['three', 'one', 'four', 'one', 'five']
> >>> x
> ['three', 'one', 'four', 'one', 'five']
> >>> list(set(x))
> ['four', 'five', 'three', 'one']

Why one *"one"* has purged out?
Removing double occurences in a list?
-- 
goto /dev/null

[toc] | [prev] | [next] | [standalone]


#5433

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-05-15 16:28 +0000
Message-ID<4dcfff15$0$29996$c3e8da3$5496439d@news.astraweb.com>
In reply to#5431
On Mon, 16 May 2011 00:05:44 +0800, TheSaint wrote:

> Chris Torek wrote:
> 
>> >>> x = ['three', 'one', 'four', 'one', 'five'] x
>> ['three', 'one', 'four', 'one', 'five']
>> >>> list(set(x))
>> ['four', 'five', 'three', 'one']
> 
> Why one *"one"* has purged out?
> Removing double occurences in a list?

Break the operation up into two steps instead of one:


>>> x = ['three', 'one', 'four', 'one', 'five']
>>> s = set(x)
>>> print s
set(['four', 'five', 'three', 'one'])
>>> list(s)
['four', 'five', 'three', 'one']


Once an element is already in a set, adding it again is a null-op:


>>> s = set()
>>> s.add(42)
>>> s.add(42)
>>> s.add(42)
>>> print s
set([42])




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#5437

FromTheSaint <nobody@nowhere.net.no>
Date2011-05-16 00:35 +0800
Message-ID<iqovdb$hk9$2@speranza.aioe.org>
In reply to#5433
Steven D'Aprano wrote:

>>>> s = set()
>>>> s.add(42)
>>>> s.add(42)
>>>> s.add(42)
>>>> print s
> set([42])

Good to know. I'll remember it

-- 
goto /dev/null

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web