Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; '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; '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; 'hash': 0.29; 'table,': 0.29; 'received:209.85.215.46': 0.30; '11,': 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; '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=nB1K3Unm0WUJ1HhbSiH0+52Bmi5JqiVw8fRLBRDY15k=; b=l8GQEnYeazAAJ5IpwNZIwPMR0vMclzJoze2Ltp2bCNG2BRJjERZWTjbVoBhWwjfMW9 jE6zuIELHPcQQON4am8XbkSyopii2/5k02v77FZsJ/nj5fhv4jYm85cx0fLf30zaWn00 j/76RRsdqoyvotZBrNq2MuOOLnPirNGxxS4ZAwFxVAuBIK2EXPdA2VVbU0zCBfm/7Xyk etKASpvC6rywVRTBY4CzyOPUaDp/IWymWiAW27AbKEZ0N6YzdIAhmos8mvoir6q+TnnL kmew7370Z/jPFGvvzTdt+xPAoAWXVC4a06+FJtu0DRr0EH/+OWM/pGSar3t2ePEOBHY/ VVhQ== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 16 Aug 2012 16:55:33 -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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345157765 news.xs4all.nl 6948 [2001:888:2000:d::a6]:49059 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27211 On Thu, Aug 16, 2012 at 12:00 PM, Aaron Brady 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 =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.