Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'explicitly': 0.05; 'subject:Python': 0.06; 'iterate': 0.09; 'keys,': 0.09; 'subject:()': 0.09; 'cc:addr:python-list': 0.11; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'modification': 0.16; 'sender:addr:gmail.com': 0.17; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'skip': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; '"",': 0.31; 'keyerror:': 0.31; 'keys': 0.31; 'file': 0.32; '(most': 0.33; "can't": 0.35; 'received:google.com': 0.35; 'add': 0.35; 'shows': 0.36; 'list': 0.37; 'issue': 0.38; 'recent': 0.39; 'delete': 0.39; 'changed': 0.39; 'temporary': 0.65; 'to:addr:gmail.com': 0.65; 'subject: & ': 0.68; 'demonstrates': 0.84; 'dict.': 0.84; 'to/from': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=xWgfgmsLMIasPjtrLelskS3GgWX0j9atXH9he/c3Ebw=; b=bzsXjJ2XIXYIDJOolgWUbIWUtMERw1pZMNCatnrEOXsK6q7bu2x8ALvzWBMaxvOYWC 2r8KTljeGSXpBf19OZYLDNazAbkjhe7w+4Eqcwm4mIb8hYT6CWwvKL0aHbUC7MTM8XMJ AlS7FHdrCVBsA1fkz6X48VbBcu6WYV4Dyuozn2G7vCmrpjPkhTUgvj8ynlj/EMdv16Y+ h80UzSaf2ABKRDmgpNjoV2Ha7pxMHSnaDc38tXkLppc48JYbFlWDg15bYvh2NhhJROp+ VblLEz6TNgOVBtd5WP2gRDhyyEkZiqM1fMf6Pygs5ezyojkGPeIHuKL2qOE0qEVMjVBY 75BA== MIME-Version: 1.0 X-Received: by 10.43.77.137 with SMTP id zi9mr18744788icb.106.1374677901221; Wed, 24 Jul 2013 07:58:21 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: References: <51EF2AD8.3080105@stoneleaf.us> Date: Wed, 24 Jul 2013 09:58:21 -0500 X-Google-Sender-Auth: ygcVLRVRW1YQxem-ttBtJJ4kFps Subject: Re: Python 3: dict & dict.keys() From: Skip Montanaro To: Oscar Benjamin Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374677910 news.xs4all.nl 15938 [2001:888:2000:d::a6]:49792 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51138 > What do you mean? Why would you want to create a temporary list just to > iterate over it explicitly or implicitly (set, sorted, max,...)? Because while iterating over the keys, he might also want to add or delete keys to/from the dict. You can't do that while iterating over them in-place. This example demonstrates the issue and also shows that the modification actually takes place: >>> d = dict(zip(range(10), range(10, 0, -1))) >>> d {0: 10, 1: 9, 2: 8, 3: 7, 4: 6, 5: 5, 6: 4, 7: 3, 8: 2, 9: 1} >>> for k in d: ... if k == 3: ... del d[k+1] ... Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration >>> for k in list(d): ... if k == 3: ... del d[k+1] ... Traceback (most recent call last): File "", line 3, in KeyError: 4 >>> d.keys() dict_keys([0, 1, 2, 3, 5, 6, 7, 8, 9]) Skip