for key in d: # For Python 3.x: for key, value in d.items(): # For Python 2.x: for key, value in d.iteritems():
The operation items() will work for both 2 and 3, but in 2 it will return a list of the dictionary’s (key, value) pairs, which will not reflect changes to the dict that happen after the items() call.
If you want the 2.x behavior in 3.x, you can call
返回 iterator 对象
Summary (with a little backstory): view* methods are a live view into the data (that will update as it updates), whereas iter* and just-plain * are more like snapshots.
viewitems，items 等同于 python2 的 viewitems
The objects returned by
dict.viewitems() are view objects.
They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.