Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51138

Re: Python 3: dict & dict.keys()

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 <skip.montanaro@gmail.com>
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 <CAHVvXxQGCFJe7ud+mwh4zhnq5F7xvHJX1pCtGCjMaFtjBwY=iQ@mail.gmail.com>
References <51EF2AD8.3080105@stoneleaf.us> <ksnrr9$k4t$1@ger.gmane.org> <CAHVvXxQGCFJe7ud+mwh4zhnq5F7xvHJX1pCtGCjMaFtjBwY=iQ@mail.gmail.com>
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 <skip@pobox.com>
To Oscar Benjamin <oscar.j.benjamin@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5040.1374677910.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


> 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 "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
>>> for k in list(d):
...   if k == 3:
...     del d[k+1]
...
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyError: 4
>>> d.keys()
dict_keys([0, 1, 2, 3, 5, 6, 7, 8, 9])

Skip

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 3: dict & dict.keys() Skip Montanaro <skip@pobox.com> - 2013-07-24 09:58 -0500

csiph-web