for k, v in mydict.iteritems(): if v is None: mydict[k] = ''
In the more general case, for example, if you add or remove keys, it may not be safe to change the structure of the container on which you work with the loop - so it would be wise to use items to loop in an independent copy of the list. but assigning a different value for this existing index does not cause any problems, so in Python 2.any it is better to use iteritems .
However, in Python3, the code throws AttributeError: 'dict' object has no attribute 'iteritems' AttributeError: 'dict' object has no attribute 'iteritems' . Use items() instead of iteritems() here.
Refer to this post.
Alex Martelli Feb 23 '10 at 1:21 2010-02-23 01:21
source share