Functionality-wise, as you have observed, views are better. Compatibility-wise, they're worse.
Some performance metrics, taken from Python 2.7.2 on a 64-bit Ubuntu machine:
>>> from timeit import timeit
Dealing with an empty dictionary:
>>> emptydict = {}
>>> timeit(lambda: emptydict.viewkeys())
0.24384498596191406
>>> timeit(lambda: list(emptydict.viewkeys()))
0.4636681079864502
>>> timeit(lambda: emptydict.iterkeys())
0.23939013481140137
>>> timeit(lambda: list(emptydict.iterkeys()))
1.0098130702972412
Constructing the view is slightly more expensive, but consuming the view is significantly faster than the iterator (a bit over twice as fast).
Dealing with a thousand-element dictionary:
>>> fulldict = {i: i for i in xrange(1000)}
>>> timeit(lambda: fulldict.viewkeys())
0.24295306205749512
>>> timeit(lambda: list(fulldict.viewkeys()))
13.447425842285156
>>> timeit(lambda: fulldict.iterkeys())
0.23759889602661133
>>> timeit(lambda: list(fulldict.iterkeys()))
15.45390510559082
Same results, though less marked; constructing the view is very slightly more expensive, but consuming it is quite definitely faster (15% faster).
For fair comparison with list(dict.viewkeys())
and list(dict.iterkeys())
, dict.keys()
is distinctlyfaster:
>>> timeit(lambda: emptydict.keys())
0.2385849952697754
>>> timeit(lambda: fulldict.keys())
7.842105150222778
Summary: it's a trade-off; better functionality (which you will rarely use) and performance (which will only very rarely be significant enough to worry you—if you're caring about such performance matters, you're probably already in the region of needing to work with numpy/scipy) versus better compatibility and muscle memory usage.
Personally, unless already depending on 2.7-only functionality or unless I am absolutely controlling the runtime environment, I would avoid dictionary views in Python 2 code. Even in these cases, my fingers still want to type iter
instead of view
, so I let 'em!