[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
>> I wish to pop/del some items out of dictionary while iterating over
>> it.
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 ite
Iterate over the keys ( for entry in adict.keys(): )
All the best,
Fuzzy
http://www.voidspace.org.uk/python/index.shtml
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] wrote:
> 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]
--
\ "I know the guy who writes all th
[EMAIL PROTECTED] wrote:
> 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
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