set and dict iteration

Ian Kelly ian.g.kelly at
Fri Aug 17 00:55:33 CEST 2012

On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady <castironpi at> 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