The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
using iterator on a dictionary
Discuss using iterator on a dictionary in the Python Programming forum on Dev Shed. using iterator on a dictionary Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 21st, 2003, 12:47 PM
|
|
I hate nerds
|
|
Join Date: Jul 2003
Posts: 540
Time spent in forums: 21 h 28 m 44 sec
Reputation Power: 0
|
|
|
using iterator on a dictionary
how do you iteratre through a dictionaries keys? i see that a dictionary has iterkeys() that returns an iterator over the ekys, but i have no idea how to use the iterator object and documentation on the web stinks
|

November 21st, 2003, 03:48 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
There are actually a few ways you can iterate over a dictionary, so you dont have to use an iterator anyway.. but heres a few working examples anyway!
Code:
dictionary = {'1': 'one', '2': 'two', '3': 'there'}
for key in dictionary.iterkeys():
print key, dictionary[key]
Code:
dictionary = {'1': 'one', '2': 'two', '3': 'there'}
for key in dictionary.keys():
print key, dictionary[key]
Code:
dictionary = {'1': 'one', '2': 'two', '3': 'there'}
for key, value in dictionary.items():
print key, value
Mark.
__________________
programming language development: www.netytan.com – Hula
|

November 21st, 2003, 04:17 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Here's another...
In python 2.2,2.3 you can iterate directly over dictionaries because they contain an iterator object. The dictionary iterator will return the next key in the un-ordered list of keys. So you can do this
Code:
a = {"tom":2.3,"****":21.1,"harry":1.1}
for key in a:
print key, a[key]
where in calls the next method of the iterator.
I just noticed you can't use *Richard* in an example!
|

November 21st, 2003, 04:23 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
There's also the dict.items(), which returns a tuple containing the key and the value.
Code:
>>> for (key, item) in {1:2,2:3,3:4}.items():
... print key, item
...
1 2
2 3
3 4
>>>
I know you asked for the keys, but it's useful sometimes to get the item straight-away.
|

November 21st, 2003, 04:34 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
i kinda already gave an example of this one Perc 
|

November 24th, 2003, 08:16 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
Hmm. Didn't see that example earlier. You sure you didn't add it after? 
|

November 24th, 2003, 09:13 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
The advantages of being mod boy  .. Naw, to my regret i cant get around that anoying last edited line
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|