More random python observations from a perl programmer

M.-A. Lemburg mal at
Sat Aug 21 13:44:56 EDT 1999

Tom Christiansen wrote:
>      [courtesy cc of this posting mailed to cited author]
> In comp.lang.python,
>     nascheme at (Neil Schemenauer) writes:
> :>    You can't use "in" on dicts.  Instead, you must use
> :In the Python philosophy, every expression should have an obvious
> :meaning.  "in" in this case does not.  Perl has a different
> :philosophy here.
> To me it would be clear that it's a wasteful linear search of the
> values, just as in a list it's a wasteful linear search of the elements.
> The point wasn't a request for a feature, just an observation of a
> conterintuitive point in something that would otherwise seem to be a
> natural effect.

There are basically three "natural" approaches to "x in dict":

1. x in dict.keys()
2. x in dict.values()
3. x in dict.items()

Since dictionaries are unordered sets of relations, the third
would be the mathematically correct one... but also the most
useless one.

In Python "o in s" is only defined for sequences s with the
meaning "there is an index i so that s[i] == o". Note that there
may well be more than one such index.

Marc-Andre Lemburg
Y2000:                                                   132 days left
Python Pages:                 

More information about the Python-list mailing list