newbie question - iterating through dictionary object

Steven Bethard steven.bethard at gmail.com
Thu Feb 17 11:54:56 EST 2005


Grant Edwards wrote:
> Here's another choice, that's sometimes handy:
> 
>>>>d = {1:'one',2:'two',3:'three'}
>>>>for k,v in d.items():
> 
> ....  print k,v
> .... 
> 1 one
> 2 two
> 3 three
> 
> I wouldn't recommend this for large dictionaries.

Yes, for large dictionaries, you should use:

for k, v in d.iteritems():
     print k, v

STeVe



More information about the Python-list mailing list