looping through the keys of a dictionary

Markus Schaber markus at schabi.de
Thu Aug 23 11:17:40 CEST 2001


Burkhard Kloss <bk at xk7.com> schrub:

>> Is there a way to loop through the keys of a dictionary (whitout
>> using a list containing all those keys)?
>>
>> e.g. mydict = {'123':[1,9,13],'125':[6,8],'225':[5]}
> for key in mydict.keys():
>     print key, mydict [key]
> 
> should do the trick

It does, but

>>> for key,value in mydict.items():
...     print key, value

seems to be faster in my eyes, as it doesn't have a dictionary lookup 
every round.

markus
-- 
1) Customers cause problems.
2) Marketing is trying to create more customers.
Therefore:
3) Marketing is evil.  (Grand Edwards in comp.lang.python)



More information about the Python-list mailing list