Editor

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

6
  1. Program 1

    double = lambda x: x * 2
    print(double(4), double("ab"))
  2. Program 2

    nums = [1, 2, 3]
    print(list(map(lambda n: n * n, nums)))
  3. Program 3

    print(list(filter(lambda n: n % 2, [1, 2, 3, 4, 5])))
  4. Program 4

    m = map(abs, [-1, -2])
    print(list(m), list(m))
  5. Program 5

    words = ["pear", "fig", "banana"]
    print(sorted(words, key=len))
  6. Program 6

    pairs = [(1, "b"), (2, "a"), (3, "b")]
    print(sorted(pairs, key=lambda p: p[1]))
  7. Program 7

    print(list(map(lambda a, b: a + b, [1, 2, 3], [10, 20])))
  8. Program 8

    print(any([0, "", None]), all([]))