Q: how to scan every element of a dictionary ?
Thomas A. Bryan
tbryan at python.com
Sat Oct 28 20:19:16 EDT 2000
Hwanjo Yu wrote:
> Doesn't the dictionary data structure support an enumeration operator to
> scan every element of it ?
...
> How to do it ?
Enumeration only works for sequences. Dictionaries are mappings, not sequences.
Generally, I iterate over the keys, values, or items in a dictionary, as
appropriate.
$ python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16) [GCC pgcc-2.91.60 19981201 (egcs-1.1.1 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> dict = {'one': 1, 'two': 2, 'three': 3}
>>> for el in dict.keys():
... print dict[el]
...
1
3
2
>>> for el in dict.items():
... print el[0], "->", el[1]
...
one -> 1
three -> 3
two -> 2
>>> for el in dict.values():
... print el
...
1
3
2
---Tom
More information about the Python-list
mailing list