01Map with lambda
main.py
nums = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, nums))
print(doubled)Output
[2, 4, 6, 8]
Functional programming in Python: lambda functions, map, filter, reduce, and sorted with custom keys for clean data processing.
nums = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, nums))
print(doubled)[2, 4, 6, 8]
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)[2, 4, 6]
from functools import reduce
nums = [1, 2, 3, 4]
product = reduce(lambda a, b: a * b, nums)
print(product)24
words = ["banana", "apple", "cherry"]
by_length = sorted(words, key=lambda w: len(w))
print(by_length)['apple', 'banana', 'cherry']