Lambda, Map and Filter: What Does This Print
What does this print? · Intermediate · about 8 min
Read each short program and type exactly what it prints, with each printed line on its own line.
Example
inc = lambda n: n + 1 print(inc(5))
prints
6Program 1
double = lambda x: x * 2 print(double(4), double("ab"))
Program 2
nums = [1, 2, 3] print(list(map(lambda n: n * n, nums)))
Program 3
print(list(filter(lambda n: n % 2, [1, 2, 3, 4, 5])))
Program 4
m = map(abs, [-1, -2]) print(list(m), list(m))
Program 5
words = ["pear", "fig", "banana"] print(sorted(words, key=len))
Program 6
pairs = [(1, "b"), (2, "a"), (3, "b")] print(sorted(pairs, key=lambda p: p[1]))
Program 7
print(list(map(lambda a, b: a + b, [1, 2, 3], [10, 20])))
Program 8
print(any([0, "", None]), all([]))