By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

Documentation for iteration in mappings

P: n/a
Hi!

I must be blind because I didn't find anything in the documentation
which says iterating over an dictionary iterates over its keys.

For example

a_dictionary = {0: "zero", 1: "one"}
for x in a:
print x

gives you

0
1

Where is this information hidden? :)

Bye,
Dennis
Oct 25 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Dennis Benzinger wrote:

I must be blind because I didn't find anything in the documentation
which says iterating over an dictionary iterates over its keys.

For example

a_dictionary = {0: "zero", 1: "one"}
for x in a:
print x

gives you

0
1

Where is this information hidden? :)


You should examine your program. Simply pasting the code above to the
Python shell fails as expected:

$ python
Python 2.4.2 (#1, Oct 1 2005, 14:32:09)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
a_dictionary = {0: "zero", 1: "one"}
for x in a: .... print x
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'a' is not defined


Ciao, Michael.
Oct 25 '05 #2

P: n/a
On Tue, 25 Oct 2005 18:02:47 +0200, Michael Ströder wrote:
Dennis Benzinger wrote:

I must be blind because I didn't find anything in the documentation
which says iterating over an dictionary iterates over its keys. [snip] Where is this information hidden? :)


You should examine your program. Simply pasting the code above to the
Python shell fails as expected:


Yes, this is true, but also irrelevant. The question isn't about the code,
but about the documentation.

The answer does appear to be hidden quite deeply. I had to dig out an old
"What's New" for version 2.2 to find it, hidden away in a section on
iterators:

http://aspn.activestate.com/ASPN/doc...00000000000000
--
Steven.

Oct 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.