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
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
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
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
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):
>
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
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):
On 2014-04-25 14:50, Terry Reedy wrote:
> If you expect to delete more than half the keys *and* if there are
> no other references to the dict, such that you need the particular
> object mutated, this might be better.
If that's your precondition, then it might be better to do something
like
kee
On 4/25/2014 2:04 PM, Matthew Barnett wrote:
On 2014-04-25 18:53, 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
d
On 2014-04-25 18:53, 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
On Sat, Apr 26, 2014 at 3: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 (a
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):
RuntimeError: dictionary changed size
12 matches
Mail list logo