Re: modify dictionary while iterating

2005-11-11 Thread François 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

Re: modify dictionary while iterating

2005-11-11 Thread skip
>> 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

Re: modify dictionary while iterating

2005-11-11 Thread Fuzzyman
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

Re: modify dictionary while iterating

2005-11-11 Thread Ben Finney
[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

Re: modify dictionary while iterating

2005-11-11 Thread Peter Otten
[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

modify dictionary while iterating

2005-11-10 Thread 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