Converting a set into list
Duncan Booth
duncan.booth at invalid.invalid
Mon May 16 06:24:28 EDT 2011
Chris Torek <nospam at 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
More information about the Python-list
mailing list