set and dict iteration

Ian Kelly ian.g.kelly at gmail.com
Thu Aug 16 18:55:33 EDT 2012


On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady <castironpi at gmail.com> wrote:
> The inconsistency is, if we remove an element from a set and add another during iteration, the new element might appear later in the iteration, and might not, depending on the hash code; therefore comparing the size of the set between iterations isn't adequate.  Example:

It can be more than just the new element.  For example, here the
entire set is repeated (Python 3.2):

>>> s = set(range(8, 13))
>>> it = iter(s)
>>> from itertools import islice
>>> list(islice(it, 5))  # avoid exhausting the iterator
[8, 9, 10, 11, 12]
>>> s.add(13)
>>> s.remove(13)
>>> list(it)
[8, 9, 10, 11, 12]

This occurs because the addition of the sixth item triggers a resize
of the underlying hash table, and the existing items, which were
originally in slots 0-4, are now in slots 8-12.



More information about the Python-list mailing list