Help with map python 2

flebber flebber.crue at gmail.com
Mon Jan 5 06:37:22 EST 2015


> In py2, map produces a list already.  In any case, above is syntax error 
> without else clause.
> 
> map(lambda x: x * -1 if x%2 else x, series)
> 
> If you do not have a function already, a list comp is better.
> 
> [(-1*k if k%2 else k) for k in range(2, N)]
> 
> Change [] to () and you have a generator expression.

Thanks Terry
[(-1*k if k%2 else k) for k in range(2, N)] 

is really short and I think pythonic being a list comprehension and works in py2 and py3.

Sayth



More information about the Python-list mailing list