[Tutor] Iterating a dict with an iteration counter? How would *you* do it?

Steven D'Aprano steve at pearwood.info
Tue Feb 5 00:18:14 CET 2013

On 05/02/13 09:26, Dave Angel wrote:

> Another point. I don't currently have Python 3.x installed, but I seem to
>remember that in Python 3 you can use the dict itself as an iterator
>providing both key and value. If I'm right, then it could be simplified
>further to:
> for i, (k, v) in enumerate(data):

Nope, in both Python 2 and 3 iterating over a dict directly just provides the
key. That's also how "if key in dict" works.


More information about the Tutor mailing list