NEWBIE: Extending a For Statement.

Wildemar Wildenburger wildemar at freakmail.de
Mon May 21 10:36:26 EDT 2007


Dustan wrote:
>>>> (key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
>>>>         
>   File "<stdin>", line 1
>     (key, mydict[key] for key in mydict if key in xrange(60, 69) or
> key == 3]
>                         ^
> SyntaxError: invalid syntax
>
> Perhaps you meant that second one to be:
> (key, mydict[key] for key in mydict if key in xrange(60, 69) or key ==
> 3)
>   
Clearly not! Its called *list*-comprehension, not tuple-comprehension. ;)
This works: [(key, mydict[key]) for key in mydict if key in xrange(60, 
69) or key ==3]
(Note the parenthesis around (key, mydict[key]), they are MENDATORY.)

regards
W



More information about the Python-list mailing list