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


Groups > comp.lang.python > #7982

Re: How to iterate on a changing dictionary

From Terry Reedy <tjreedy@udel.edu>
Subject Re: How to iterate on a changing dictionary
Date 2011-06-19 12:51 -0400
References <itl1a5$rom$1@speranza.aioe.org> <mailman.154.1308496441.1164.python-list@python.org> <roy-3DDD62.11534419062011@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.161.1308502281.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-19 22:32 +0800
  Re: How to iterate on a changing dictionary Chris Angelico <rosuav@gmail.com> - 2011-06-20 01:13 +1000
    Re: How to iterate on a changing dictionary Roy Smith <roy@panix.com> - 2011-06-19 11:53 -0400
      Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:51 -0400
  Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:02 -0400
  Re: How to iterate on a changing dictionary Lie Ryan <lie.1296@gmail.com> - 2011-06-20 03:00 +1000
    Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-20 21:20 +0800
      Re: How to iterate on a changing dictionary Florencio Cano <florencio.cano@gmail.com> - 2011-06-20 16:30 +0200
      Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-20 13:37 -0400
        Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-21 18:44 +0800

csiph-web