set and dict iteration
Aaron Brady
castironpi at gmail.com
Fri Aug 17 13:47:16 EDT 2012
On Thursday, August 16, 2012 6:07:40 PM UTC-5, Ian wrote:
> On Thu, Aug 16, 2012 at 4:55 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>
> > 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.
>
>
>
> Another curious example:
>
>
>
> >>> s = set(range(8, 48, 8))
>
> >>> s
>
> {8, 16, 40, 24, 32}
>
> >>> it = iter(s)
>
> >>> from itertools import islice
>
> >>> list(islice(it, 4))
>
> [8, 16, 40, 24]
>
> >>> s.add(48)
>
> >>> s.remove(48)
>
> >>> list(it)
>
> [8, 16, 40, 24]
>
>
>
> Hey, what happened to 32?
Good examples. The former occurs without the 'islice' as well.
s= set( range( 8, 13 ) )
it= iter( s )
print( [ next( it ) for _ in range( 5 ) ] )
s.add( 13 )
s.remove( 13 )
print( [ next( it ) for _ in range( 5 ) ] )
Output:
[8, 9, 10, 11, 12]
[8, 9, 10, 11, 12]
More information about the Python-list
mailing list