Comprehensions & Generators · Question 6 · Easy

The built-in len is passed to map for a list of city names. What does this code print?

print(list(map(len, ["goa", "delhi", "agra"])))
  1. [3, 5, 4]
  2. 12
  3. [5, 4, 3]
  4. ['goa', 'delhi', 'agra']
Answer and explanation

Answer: A. [3, 5, 4]

Explanation: map applies len to each string in turn, giving 3, 5 and 4 in the original order. list() collects those values into a list.

Previous: Which of these expressions creates a generator object rather than a fully… · Next: What does calling next(g) do when g is a generator that still has values left?

Practise comprehensions & generators

Try a fresh set of comprehensions & generators questions with an explanation after every answer, free and without signing up: Comprehensions & Generators practice. Or sit a full placement mock test.

More comprehensions & generators questions

More python practice

Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge