Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.07; 'subject:changing': 0.07; 'terry': 0.07; 'python': 0.08; 'empty,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'am,': 0.14; 'wrote:': 0.14; '39,': 0.16; '52,': 0.16; 'dictionaries': 0.16; 'reedy': 0.16; 'roy': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'memory': 0.22; 'keys': 0.23; 'code': 0.24; 'handles': 0.26; "i'm": 0.27; 'lists': 0.29; 'subject:How': 0.30; 'yet': 0.32; 'header:X-Complaints-To:1': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'too': 0.33; 'header:User-Agent:1': 0.35; 'another': 0.37; 'received:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'header :Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'delete': 0.40; 'more': 0.60; 'your': 0.60; 'believe': 0.66; '26,': 0.67; '11:53': 0.84; '78,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: How to iterate on a changing dictionary Date: Sun, 19 Jun 2011 12:51:06 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: 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: 20 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308502282 news.xs4all.nl 49175 [::ffff:82.94.164.166]:38384 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7982 On 6/19/2011 11:53 AM, Roy Smith wrote: > Yet another variation which makes sense if you want to delete most of > the keys would be to copy them to a new dictionary. I'm not sure how > Python handles memory management on dictionaries which shrink. 'Python' does not handle memory management; each implementation does -). I believe CPython will shrink lists and dicts that are too empty, but it should be both more efficient and more dependable to make a new one as with your code or old_d = {i:i for i in range(100)} d = {key:val for key,val in old_d.items() if not key%13} d >>> {0: 0, 65: 65, 39: 39, 13: 13, 78: 78, 52: 52, 26: 26, 91: 91} -- Terry Jan Reedy