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

Dave Angel davea at davea.name
Tue Feb 5 01:04:07 CET 2013


On 02/04/2013 06:18 PM, Steven D'Aprano wrote:
> 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.
>

Then I'm glad I was tentative about it.  I do recall there was some 
difference.  Was it just that items(), keys() and values() methods 
return a view (iterator) instead of a list, and the iter*() versions are 
gone?

-- 
DaveA


More information about the Tutor mailing list