Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Converting a set into list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  22 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
TheSaint  
View profile  
 More options May 14 2011, 5:02 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Sat, 14 May 2011 17:02:48 +0800
Local: Sat, May 14 2011 5:02 am
Subject: Converting a set into list
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options May 14 2011, 5:33 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Sat, 14 May 2011 11:33:15 +0200
Local: Sat, May 14 2011 5:33 am
Subject: Re: Converting a set into list

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options May 14 2011, 10:12 am
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Sun, 15 May 2011 00:12:31 +1000
Local: Sat, May 14 2011 10:12 am
Subject: Re: Converting a set into list

TheSaint <nob...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TheSaint  
View profile  
 More options May 14 2011, 10:14 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Sat, 14 May 2011 22:14:40 +0800
Local: Sat, May 14 2011 10:14 am
Subject: Re: Converting a set into list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TheSaint  
View profile  
 More options May 14 2011, 10:51 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Sat, 14 May 2011 22:51:30 +0800
Local: Sat, May 14 2011 10:51 am
Subject: Re: Converting a set into list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options May 14 2011, 2:22 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sun, 15 May 2011 04:22:33 +1000
Local: Sat, May 14 2011 2:22 pm
Subject: Re: Converting a set into list

On Sun, May 15, 2011 at 12:14 AM, TheSaint <nob...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options May 14 2011, 7:21 pm
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Sun, 15 May 2011 09:21:38 +1000
Local: Sat, May 14 2011 7:21 pm
Subject: Re: Converting a set into list

TheSaint <nob...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Torek  
View profile  
 More options May 14 2011, 10:11 pm
Newsgroups: comp.lang.python
From: Chris Torek <nos...@torek.net>
Date: 15 May 2011 02:11:11 GMT
Local: Sat, May 14 2011 10:11 pm
Subject: Re: Converting a set into list
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SigmundV  
View profile  
 More options May 15 2011, 7:18 am
Newsgroups: comp.lang.python
From: SigmundV <sigmu...@gmail.com>
Date: Sun, 15 May 2011 04:18:46 -0700 (PDT)
Local: Sun, May 15 2011 7:18 am
Subject: Re: Converting a set into list
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SigmundV  
View profile  
 More options May 15 2011, 7:23 am
Newsgroups: comp.lang.python
From: SigmundV <sigmu...@gmail.com>
Date: Sun, 15 May 2011 04:23:40 -0700 (PDT)
Local: Sun, May 15 2011 7:23 am
Subject: Re: Converting a set into list
I'm sorry I top posted. I'll remember not to top post next time.

Sigmund


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TheSaint  
View profile  
 More options May 15 2011, 11:56 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Sun, 15 May 2011 23:56:58 +0800
Local: Sun, May 15 2011 11:56 am
Subject: Re: Converting a set into list

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TheSaint  
View profile  
 More options May 15 2011, 12:05 pm
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Mon, 16 May 2011 00:05:44 +0800
Local: Sun, May 15 2011 12:05 pm
Subject: Re: Converting a set into list

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options May 15 2011, 12:28 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 15 May 2011 16:28:05 GMT
Local: Sun, May 15 2011 12:28 pm
Subject: Re: Converting a set into list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TheSaint  
View profile  
 More options May 15 2011, 12:35 pm
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Mon, 16 May 2011 00:35:55 +0800
Local: Sun, May 15 2011 12:35 pm
Subject: Re: Converting a set into list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options May 15 2011, 1:07 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sun, 15 May 2011 13:07:25 -0400
Local: Sun, May 15 2011 1:07 pm
Subject: Re: Converting a set into list
In article
<34fc571c-f382-405d-94b1-0a673da5f...@t16g2000vbi.googlegroups.com>,

 SigmundV <sigmu...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Rachel  
View profile  
 More options May 15 2011, 4:27 pm
Newsgroups: comp.lang.python
From: Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa...@spamschutz.glglgl.de>
Date: Sun, 15 May 2011 22:27:34 +0200
Local: Sun, May 15 2011 4:27 pm
Subject: Re: Converting a set into list
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Kluev  
View profile  
 More options May 15 2011, 10:37 pm
Newsgroups: comp.lang.python
From: Daniel Kluev <dan.kl...@gmail.com>
Date: Mon, 16 May 2011 13:37:17 +1100
Local: Sun, May 15 2011 10:37 pm
Subject: Re: Converting a set into list

> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options May 16 2011, 2:34 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Mon, 16 May 2011 08:34:44 +0200
Local: Mon, May 16 2011 2:34 am
Subject: Re: Converting a set into list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Duncan Booth  
View profile  
 More options May 16 2011, 6:24 am
Newsgroups: comp.lang.python
From: Duncan Booth <duncan.bo...@invalid.invalid>
Date: 16 May 2011 10:24:28 GMT
Local: Mon, May 16 2011 6:24 am
Subject: Re: Converting a set into list

Chris Torek <nos...@torek.net> wrote:
>     >>> 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.

A minor change to your example makes it out of order even for integers:

>>> x = [7, 8, 9, 1, 4, 1]
>>> list(set(x))

[8, 9, 1, 4, 7]

or for that mattter:

>>> list(set([3, 32, 4, 32, 5, 9, 2, 6]))

[32, 2, 3, 4, 5, 6, 9]

--
Duncan Booth http://kupuguy.blogspot.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
TheSaint  
View profile  
 More options May 16 2011, 10:23 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: TheSaint <nob...@nowhere.net.no>
Date: Mon, 16 May 2011 22:23:07 +0800
Local: Mon, May 16 2011 10:23 am
Subject: Re: Converting a set into list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options May 16 2011, 8:33 pm
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Tue, 17 May 2011 10:33:16 +1000
Local: Mon, May 16 2011 8:33 pm
Subject: Re: Converting a set into list

TheSaint <nob...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Torek  
View profile  
 More options May 16 2011, 9:07 pm
Newsgroups: comp.lang.python
From: Chris Torek <nos...@torek.net>
Date: 17 May 2011 01:07:41 GMT
Local: Mon, May 16 2011 9:07 pm
Subject: Re: Converting a set into list

>Chris Torek <nos...@torek.net> wrote:
>>     >>> x = [3, 1, 4, 1, 5, 9, 2, 6]
>>     >>> list(set(x))
>> This might not be the best example since the result is sorted
>> "by accident", while other list(set(...)) results are not.

In article <Xns9EE772D313153duncanbo...@127.0.0.1>,
Duncan Booth  <duncan.bo...@suttoncourtenay.org.uk> wrote:

>A minor change to your example makes it out of order even for integers:

>>>> x = [7, 8, 9, 1, 4, 1]
>>>> list(set(x))
>[8, 9, 1, 4, 7]

>or for that mattter:

>>>> list(set([3, 32, 4, 32, 5, 9, 2, 6]))
>[32, 2, 3, 4, 5, 6, 9]

Yes, but then it is no longer "as easy as pi". :-)
--
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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »