Modifying list/dictionary elements whilst iterating???
Philip Payne
pnpayne at swissonline.ch
Thu Feb 17 10:01:24 EST 2000
Hi,
I know it's not allowed to add/remove elements from lists/dictionaries
whilst iterating over them, but can someone give me a definitive answer as
to whether modifying list/dictionary values as follows whilst iterating is
safe:
d = {'the' : 1, 'cat' : 2, 'sat' : -4}
print 'before', d
for key,value in d.items() :
value = value + 1
d[key] = value
print 'after', d
l = [1,2,3,4,5]
print 'before', l
for i in xrange(len(l)) :
l[i] = l[i] + 1
print 'after', l
Regards,
Philip Payne
More information about the Python-list
mailing list