More random python observations from a perl programmer
mal at lemburg.com
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 ucalgary.ca (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
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.
Y2000: 132 days left
Python Pages: http://www.lemburg.com/python/
More information about the Python-list