Having read this post on the difference between the "iter-" and "view-" methods of dictionary iteration, I'm still curious about the advantages/disadvantages between running for i in d: ...
vs. for i in d.viewkeys(): ...
. Is the first snippet just shorthand for the second? Please help this curious and pedantic python lover :)
Asked
Active
Viewed 358 times
0

U13-Forward
- 69,221
- 14
- 89
- 114

cscn
- 3
- 1
-
I removed the `python-3.x` tag because I believe this is a python2 question? – rafaelc Aug 15 '18 at 01:48
-
Ah yes, sorry. I forgot that `d.viewkeys` is the python 2.x port of python 3.x's `d.keys`. – cscn Aug 15 '18 at 01:50
3 Answers
2
From the point-of-view of iteration, there is no difference, since the iter
method returns the same type of iterator for both. This makes sense since viewkeys
returns a view:
>>> d = {}
>>> iter(d.viewkeys())
<dictionary-keyiterator object at 0x102567cb0>
>>> iter(d)
<dictionary-keyiterator object at 0x102567d08>
>>>
The only difference is the small, constant-time overhead of creating the view object.
.viewkeys
is useful if you want the set-like behavior, e.g.:
>>> d1 = {'a':1,'b':2}
>>> d2 = {'a':2, 'b':1, 'c':3}
>>> d1.viewkeys() | d2.viewkeys()
set(['a', 'c', 'b'])
>>>
If you want to iterate over the keys of a dictionary, just iterate over the dictionary directly.

juanpa.arrivillaga
- 88,713
- 10
- 131
- 172
0
Timings:
from timeit import timeit
print timeit(lambda: {i:i for i in range(10)}.keys())
print timeit(lambda: {i:i for i in range(10)}.iterkeys())
print timeit(lambda: {i:i for i in range(10)}.viewkeys())
print timeit(lambda: list({i:i for i in range(10)}))
Output:
0.890981912613
0.622451066971
0.619216918945
1.39578795433
The winner is .viewkeys()
but similar result to .iterkeys()
, both are good.
Iteration Timing:
import time
s=time.time()
for i in d:
pass
e=time.time()
print e-s
s=time.time()
for i in d.viewkeys():
pass
e=time.time()
print e-s
s=time.time()
for i in d.iterkeys():
pass
e=time.time()
print e-s
s=time.time()
for i in d.keys():
pass
e=time.time()
print e-s
Output:
1.90734863281e-06
2.86102294922e-06
9.53674316406e-07
3.09944152832e-06

Community
- 1
- 1

U13-Forward
- 69,221
- 14
- 89
- 114
-
This doesn't show anything about iteration... these timings are dominated by the time it takes to create the dictionary... – juanpa.arrivillaga Aug 15 '18 at 02:03
-
-
These timings don't demonstrate much, the dictionary is so small and you are timing a single loop, that they, again, are probably dominated by the method-call and noise due to your computer's other processes. – juanpa.arrivillaga Aug 15 '18 at 02:12
-
@juanpa.arrivillaga Yeah you're actually right, i couldn't test it with a bigger dictionary because i am on python 3.6.0, i have to test it on tutorials-point code editor and i tried with bigger dict but tutorials-point doesn't allow it because it is a to bige dictionary for it to handle – U13-Forward Aug 15 '18 at 02:15
-
1
-
@juanpa.arrivillaga Thanks for letting me know that site i'll consider using that next time – U13-Forward Aug 15 '18 at 02:18
-2
Consider this exmaple:
data = {"a": "123", "b": "456"}
for i in data:
print i
for i in data.viewkeys():
print i
print data.viewkeys()
print data
Output:
a
b
a
b
dict_keys(['a', 'b'])
{'a': '123', 'b': '456'}
From the loop prospective,
for i in d: ...
will loop through actual dictionary, ex:{'a': '123', 'b': '456'}
for i in d.viewkeys(): ...
will loop throughlist
of keys, ex:['a', 'b']
viewkeys
method will be useful if you need list of keys from your dictionary

Nishant
- 797
- 5
- 5
-
Incorrect. `viewkeys` returns a set-like *view* of keys in the `dict`. – juanpa.arrivillaga Aug 15 '18 at 02:02
-