Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; '16,': 0.03; '(python': 0.05; 'repeated': 0.07; '40,': 0.09; 'subject:set': 0.09; 'to:addr:comp.lang.python': 0.09; 'underlying': 0.09; 'cc:addr:python-list': 0.10; 'aug': 0.13; '12]': 0.16; '24,': 0.16; '24]': 0.16; 'element.': 0.16; 'iteration,': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'resize': 0.16; 'later': 0.16; 'wrote:': 0.17; 'comparing': 0.17; 'element': 0.17; 'thu,': 0.17; '>>>': 0.18; 'import': 0.21; 'not,': 0.21; 'occurs': 0.22; 'originally': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'cc:addr:gmail.com': 0.27; '>>>>': 0.29; 'hash': 0.29; 'table,': 0.29; 'thursday,': 0.30; '11,': 0.33; 'curious': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'item': 0.37; 'received:209': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'remove': 0.61; 'between': 0.63; 'more': 0.63; 'here': 0.65; 'therefore': 0.65; 'august': 0.66; 'hey,': 0.72; 'examples.': 0.84; 'sixth': 0.84; 'items,': 0.91; 'received:209.85.220.184': 0.91 Newsgroups: comp.lang.python Date: Fri, 17 Aug 2012 10:47:16 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=67.184.78.189; posting-account=MQ3pigoAAACeFzUFjVAePnOjOJMNlvq9 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 67.184.78.189 MIME-Version: 1.0 Subject: Re: set and dict iteration From: Aaron Brady To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, Aaron Brady X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345226405 news.xs4all.nl 6961 [2001:888:2000:d::a6]:52436 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27249 On Thursday, August 16, 2012 6:07:40 PM UTC-5, Ian wrote: > On Thu, Aug 16, 2012 at 4:55 PM, Ian Kelly wrote: >=20 > > On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady wr= ote: >=20 > >> The inconsistency is, if we remove an element from a set and add anoth= er during iteration, the new element might appear later in the iteration, a= nd might not, depending on the hash code; therefore comparing the size of t= he set between iterations isn't adequate. Example: >=20 > > >=20 > > It can be more than just the new element. For example, here the >=20 > > entire set is repeated (Python 3.2): >=20 > > >=20 > >>>> s =3D set(range(8, 13)) >=20 > >>>> it =3D iter(s) >=20 > >>>> from itertools import islice >=20 > >>>> list(islice(it, 5)) # avoid exhausting the iterator >=20 > > [8, 9, 10, 11, 12] >=20 > >>>> s.add(13) >=20 > >>>> s.remove(13) >=20 > >>>> list(it) >=20 > > [8, 9, 10, 11, 12] >=20 > > >=20 > > This occurs because the addition of the sixth item triggers a resize >=20 > > of the underlying hash table, and the existing items, which were >=20 > > originally in slots 0-4, are now in slots 8-12. >=20 >=20 >=20 > Another curious example: >=20 >=20 >=20 > >>> s =3D set(range(8, 48, 8)) >=20 > >>> s >=20 > {8, 16, 40, 24, 32} >=20 > >>> it =3D iter(s) >=20 > >>> from itertools import islice >=20 > >>> list(islice(it, 4)) >=20 > [8, 16, 40, 24] >=20 > >>> s.add(48) >=20 > >>> s.remove(48) >=20 > >>> list(it) >=20 > [8, 16, 40, 24] >=20 >=20 >=20 > Hey, what happened to 32? Good examples. The former occurs without the 'islice' as well. s=3D set( range( 8, 13 ) )=20 it=3D iter( s )=20 print( [ next( it ) for _ in range( 5 ) ] ) s.add( 13 )=20 s.remove( 13 )=20 print( [ next( it ) for _ in range( 5 ) ] ) Output: [8, 9, 10, 11, 12] [8, 9, 10, 11, 12]