veit/python-basics-tutorial

Sets iterate in arbitrary order

Closed this issue · 3 comments

veit commented

They can often appear ordered, but this is not mandatory:

>>> s1 = set(range(19))
>>> s2 = set(range(19))
>>> for i in range(1, 19, 2): s2.remove(i)
... 
>>> for i in range(1, 19, 2): s2.add(i)
... 
>>> s2
{0, 2, 4, 6, 8, 9, 10, 7, 12, 5, 14, 1, 16, 3, 18, 11, 13, 15, 17}
>>> s1 == s2
True
veit commented

@krother: I’m not sure whether I should include this in the set documentation. On the one hand, it shows nicely that sets can be unsorted, but on the other hand it preempts set comprehension.

in my opinion this is not necessary; the example is interesting but a special case. The remark that sets are not ordered should be sufficient.

veit commented

@krother: Thank you for your opinion. Then I will close the ticket at this point.