Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-28 Thread Duncan Booth
Chris Angelico wrote: > # Snapshot of keys: > for k in list(d): > if f(k): del d[k] > > No extra loop at the end, no switching out and in of contents, just > one little change in the loop header. Obviously you don't want to do > this when you're deleting two out of three billion, but for sma

Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-26 Thread Roy Smith
In article <535c67e9$0$29965$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > I think the two obviously good enough approaches are: > > - save a "to be deleted" list, then delete those keys; > > - copy the "not to be deleted" items into a new dict There is a third possibility: I

Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-26 Thread Chris Angelico
On Sun, Apr 27, 2014 at 12:14 PM, Steven D'Aprano wrote: > I think the two obviously good enough approaches are: > > - save a "to be deleted" list, then delete those keys; > > - copy the "not to be deleted" items into a new dict For a small enough dict that the performance question doesn't matter

Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-26 Thread Steven D'Aprano
On Sat, 26 Apr 2014 12:25:27 -0700, Charles Hixson wrote: > On 04/25/2014 10:53 AM, Charles Hixson wrote: >> What is the proper way to delete selected items during iteration of a >> map? What I want to do is: >> >> for (k, v) in m.items(): >>if f(k): >> # do some processing of v and sa

Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-26 Thread Tim Chase
On 2014-04-26 12:25, Charles Hixson wrote: > I expect that I'll be deleting around 1/3 during > each iteration of the process...and then adding new ones back in. > There shouldn't be a really huge number of deletions on any > particular pass, but it will be looped through many times... If you have

Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-26 Thread Charles Hixson
On 04/25/2014 10:53 AM, Charles Hixson wrote: What is the proper way to delete selected items during iteration of a map? What I want to do is: for (k, v) in m.items(): if f(k): # do some processing of v and save result elsewhere del m[k] But this gives (as should be expected):