joedirt joedirt - 1 year ago 69
Python Question

comparison ex of list comprehension and lambda

Celsius = [66.5,45.2,33.5,55.5]
Fahrenheit = [((float(9)/5)*x + 32) for x in Celsius]

How would I write this in a lambda function? Ex: lambda x,y:x+y

Answer Source

Do you mean this?

Fahrenheit = list(map(lambda x: x * 9.0 / 5 + 32, Celsius))

In general, list comprehension (what your example does) can be converted to a combination of map and a lambda (or other function).


You could also use lambda x: (float(9)/5)*x + 32; I was just trying to simplify the expression. :-)