[Python-3000] Iterating over a dict

Thomas Lotze thomas.lotze at gmx.net
Sat Apr 1 14:32:19 CEST 2006


Hi,

I wonder what's the reason for iterating over a dict by keys:

>>> for x in {1:"a", 2:"b"}:
...     print x
... 
1
2

I find it much more intuitive for the values, "a" and "b", to be accessed.
This is particularly confusing as iterating over tuples, lists and sets in
the same way does access the values. (It feels like iterating over a list
l actually iterates over the index values, range(len(l)).) In fact,
iterating over any container should access the contained values.

The reason I ask this on the python-3000 list is that I wonder whether the
iterating behaviour of dicts might be changed in Python 3k, so that in the
above code, foo() would be applied to the dict's values.

-- 
Thomas




More information about the Python-3000 mailing list