modify dictionary while iterating

S

s99999999s2003

hi
I wish to pop/del some items out of dictionary while iterating over it.
a = { 'a':1, 'b':2 }
for k, v in a.iteritems():
if v==2:
del a[k]

the output say RuntimeError: dictionary changed size during iteration
how can i suppress this message in an actual script and still get the
final value of the dict?
is it something like try/except/else statment?
try:
for v,k in a.iteritems():
if v==something:
del a[k]
except RuntimeError:
< don't know what to do here>
else:
< should i include this part ? >

what other ways can i do this ? thanks for any help.
 
P

Peter Otten

hi
I wish to pop/del some items out of dictionary while iterating over it.
a = { 'a':1, 'b':2 }
for k, v in a.iteritems():
if v==2:
del a[k]

the output say RuntimeError: dictionary changed size during iteration
how can i suppress this message in an actual script and still get the
final value of the dict?
is it something like try/except/else statment?
try:
for v,k in a.iteritems():
if v==something:
del a[k]
except RuntimeError:
< don't know what to do here>
else:
< should i include this part ? >

what other ways can i do this ? thanks for any help.

If you expect to delete only a few items:
a = dict(a=1, b=2, c=3, d=2)
delenda = [k for k, v in a.iteritems() if v == 2]
for k in delenda:
.... del a[k]
....{'a': 1, 'c': 3}

If you expect to delete most items:
{'a': 1, 'c': 3}

or (if rebinding a is not an option)
.... if v == 2:
.... del a[k]
....{'a': 1, 'c': 3}

Peter
 
B

Ben Finney

I wish to pop/del some items out of dictionary while iterating over
it.

Iterate over a copy.

a_orig = { 'a': 1, 'b': 2 }
a = dict(a_orig)
for k, v in a_orig.iteritems():
if v == 2:
del a[k]
 
S

skip

I wish to pop/del some items out of dictionary while iterating over
Ben> Iterate over a copy.

Ben> a_orig = { 'a': 1, 'b': 2 }
Ben> a = dict(a_orig)
Ben> for k, v in a_orig.iteritems():
Ben> if v == 2:
Ben> del a[k]

Or iterate over just a copy of the keys:

for k in a_orig.keys():
if a_orig[k] == 2:
del a_orig[k]

Skip
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[[email protected]]
I wish to pop/del some items out of dictionary while iterating over it.
a = { 'a':1, 'b':2 }
for k, v in a.iteritems():
if v==2:
del a[k]

A simple change would be using "items()" instead of "iteritems()".

Or else, you may prefer to loop over keys, and retrieve values, either:

for k in a.keys():
if a[k] == 2:
del a[k]

or:

for k in set(a):
if a[k] == 2:
del a[k]

But in no way, you may directly iterate over the original dictionary
while altering its keys, this is explicitly forbidden in Python.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top