Iterating over dictionary items(), values(), keys() in Python 3

Viewed 98985

If I understand correctly, in Python 2, iter(d.keys()) was the same as d.iterkeys(). But now, d.keys() is a view, which is in between the list and the iterator. What's the difference between a view and an iterator?

In other words, in Python 3, what's the difference between

for k in d.keys()
    f(k)

and

for k in iter(d.keys())
    f(k)

Also, how do these differences show up in a simple for loop (if at all)?

1 Answers
Related