[Python-Dev] re: Using lists as sets
Jeremy Hylton
jeremy@cnri.reston.va.us
Mon, 20 Mar 2000 12:28:28 -0500 (EST)
>>>>> "GVW" == gvwilson <gvwilson@nevex.com> writes:
GVW> On a semi-related note, can someone explain why programs are
GVW> not allowed to iterate directly through the elements of a
GVW> dictionary:
GVW> for (key, value) in dict:
...body...
Pythonic design rules #2:
Explicit is better than implicit.
There are at least three "natural" ways to interpret "for ... in dict:"
In addition to the version that strikes you as most natural, some
people also imagine that a for loop should iterate over the keys or the
values. Instead of guessing, Python provides explicit methods for
each possibility: items, keys, values.
Yet another possibility, implemented in early versions of JPython and
later removed, was to treat a dictionary exactly like a list: Call
__getitem__(0), then 1, ..., until a KeyError was raised. In other
words, a dictionary could behave like a list provided that it had
integer keys.
Jeremy