Home Posts Topics Members FAQ
Post your question to a community of 470,811 developers. It's quick & easy.
Hello, Can we impose if then else into list comprehension ? Like we do in lambda-map form: This code change None into 0 L = [None, 12] R = map(lambda x: (x==None and [0] or x)[0], L) # [0,12]
Hello, On 24 Jun 2005 11:45:14 -0700, aj****@gmail.com wrote: Hello, Can we impose if then else into list comprehension ? Like we do in lambda-map form: This code change None into 0 L = [None, 12] R = map(lambda x: (x==None and [0] or x)[0], L) # [0,12] Do you mean: [(x==None and [0] or [x])[0] for x in L] or [{None:0}.get(x,x) for x in L] or [x or 0 for x in L] Well, the third solution doesn't exactly fit the specification but may be easier to read.
Replies have been disabled for this discussion.