Path: csiph.com!usenet.pasdenom.info!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; '16,': 0.03; '(python': 0.05; 'repeated': 0.07; '40,': 0.09; 'received :mail-lpp01m010-f46.google.com': 0.09; 'subject:set': 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; 'cc:2**0': 0.23; 'originally': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'appear': 0.26; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'hash': 0.29; 'table,': 0.29; 'received:209.85.215.46': 0.30; '11,': 0.33; 'curious': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'item': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'remove': 0.61; 'between': 0.63; 'more': 0.63; 'here': 0.65; 'therefore': 0.65; 'hey,': 0.72; 'sixth': 0.84; 'items,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=MScSXyyaO18agmDfE/6ATrJrRn9nGk2GSkNp8e0VkQI=; b=vcO/A6QxWXzBJs+HiP51ddrdTrtVWvxjSvsf0cH4zXFU4NFIT60Ces/wGBzD0jXGYO Ef2Ptt38XbmvK7t8vQfcxSpFSl/Mih56usYVskRFf+65GQcapldwjDCaeOSgp9L3nBph 8TtfbWWmfX5QItk5LgpUVicqBzabaSAWxM3c/ZO9dTGMh2Ym57fDFVsrrgA28vJZhzaE HWN1ZpxThmVBN+9TW5un9EQONrWTleCwQxY0NNnK2VFKFQpSUY+9w88pdGxVNTQfg4WH vAr3LOMLLunq8/wnxwhLV4wZ/45l2s2rECLqxZ9SOEATgl6l5furZv/5vKtlDM2jcPGc 75/A== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 16 Aug 2012 17:07:40 -0600 Subject: Re: set and dict iteration To: Aaron Brady Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345158493 news.xs4all.nl 6879 [2001:888:2000:d::a6]:51920 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27212 On Thu, Aug 16, 2012 at 4:55 PM, Ian Kelly wrote: > On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady wrot= e: >> 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 =3D set(range(8, 13)) >>>> it =3D 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 =3D set(range(8, 48, 8)) >>> s {8, 16, 40, 24, 32} >>> it =3D 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?