Re:Python: The Dictionary Playbook
by andreasvc on Wednesday January 02, 2013 @07:29PM
(#5000318)
Or dictionary views: they stay current as the dictionary is updated, and allow set operations.
[ Parent
| on HN ]
Have used all of the techniques in the article (plus dict/set comprehensions) but had never heard of dict views [1] until now. Good stuff!
[1] http://docs.python.org/2/library/stdtypes.html#dictionary-vi...
I don't get it. Why would I use a view instead of the dictionary itself?
http://stackoverflow.com/questions/340850/python-3-0-dict-me...
Views are lighter than a full copy of a list, yet behaves like a list (eg: supports `key in view`).
[edit] Also, this seems to be the relevant PEP:
It's not meant as a replacement; views form the new implementation of the keys(), values(), and items() methods in Python 3. The have been made available in Python 2 as viewkeys() etc. As said, they have the advantage of providing set operations (e.g., intersection with the keys of another dictionary), and they are more efficient than keys() or iterkeys(), because they have less overhead.