Ordering python sets

Mr.SpOOn mr.spoon21 at gmail.com
Wed Oct 22 10:48:20 EDT 2008


On Wed, Oct 22, 2008 at 4:30 PM, Roy Smith <roy at panix.com> wrote:
> In article <mailman.2839.1224683382.3487.python-list at python.org>,
>  Mr.SpOOn <mr.spoon21 at gmail.com> wrote:
>
>> It seems to me that it orders elements when you add using the add()
>> method, but if you create a set starting from a list, it may result
>> unordered.
>
> Arrrggghhh!  None of these behaviors are guaranteed.  The docs say, "A set
> object is an unordered collection".  If you write code which depends on a
> set preserving order, are going to get burned.

Yes, of course :D
I wasn't going to count on that.

> If you want something that preserves order, use a list.  If the O(n) lookup
> time of a list is too slow, you can get O(log n) with heapq.

Well, maybe I can just do it using sets and the sorted() method. If
this doesn't satisfy me, I think I'll just use lists.



More information about the Python-list mailing list