Tutorial Python Lambda

Posted in Uncategorized

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

The way you spell “lambda” is to remember to spell the animal “lamb” first. Lambda are just anonymous functions that are used once in-line — like Javascript anonymous functions.

The map function expects a function, so we can pass in a lambda instead …

doubles = list( map(lambda x : x*2, [0, 1, 2, 3])))
# [0, 2, 4, 6]

Since the map function returns a map object with use list() to turn it into something useful (like a list).

This above is equivalent to list comprehension …

doubles = [ num*2 for num in [0, 1, 2, 3]]

Similarly, the filter function takes a lambda…

evens = list(filter(lambda x : x % 2 == 0, [0, 1, 2, 3]))


Related Posts

Tags

Share This