dictionaries and 'in' in 2.1

Gerhard Häring gh_pythonlist at gmx.de
Sat Nov 3 15:03:17 EST 2001


On Sat, Nov 03, 2001 at 08:46:08PM +0000, John J. Lee wrote:
> 
> The Python 2.1.1 manual, section 2.1.6, says:
> 
>   k in a        1 if a has a key k, else 0
> k not in a      0 if a has a key k, else 1
> 
> 
> But when I attempt to use this feature, I get:
> 
> Python 2.1.1 (#1, Oct 23 2001, 15:59:13)
> [GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> d = {1:2}
> >>> 1 in d
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'in' or 'not in' needs sequence right argument
> >>>
> 
> 
> I must be doing something stupid, but I don't see what.

Are you sure you have been reading the 2.1.1 manual and not the
development version of the docs? AFAIK the x in dict form is only
available in Python 2.2. What you want in earlier versions is probably:
1 in d.keys() 

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list