The dictionary methods
keys(), values(), and items() are different in Python 3.x. They return an object called a view instead of a fully materialized list.
It’s not possible to change the return values of
keys(), values(), and items() in Python 2.7 because too much code would break. Instead the 3.x versions were added under the new names viewkeys(), viewvalues(), and viewitems().
Views can be iterated over, but the key and item views also behave like sets. The
& operator performs intersection, and | performs a union:
The view keeps track of the dictionary and its contents change as the dictionary is modified:
However, note that you can’t add or remove keys while you’re iterating over the view:
No comments:
Post a Comment