4 ms·
I hate to be that guy but what about dictionary comprehensions?
by hartror 14y ago
I hate to be that guy but what about dictionary comprehensions?
- andreasvc 14y agoOr dictionary views: they stay current as the dictionary is updated, and allow set operations.
- dougws 14y agoHave 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-view-objects http://docs.python.org/2/library/stdtypes.html#dictionary-vi...
- te 14y agoI don't get it. Why would I use a view instead of the dictionary itself?
- e12e 14y agohttp://stackoverflow.com/questions/340850/python-3-0-dict-methods-return-views-why 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: http://www.python.org/dev/peps/pep-3106/ http://www.python.org/dev/peps/pep-3106/
- andreasvc 14y agoIt'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.