Comprehensions & Generators · Question 4 · Easy

A set comprehension collects the first letter of each word. What does len report?

words = ["apple", "amla", "banana", "ber"]
print(len({w[0] for w in words}))
  1. 4
  2. 3
  3. 2
  4. 1
Answer and explanation

Answer: C. 2

Explanation: The first letters are a, a, b, b. A set keeps only distinct values, so it becomes {'a', 'b'} with length 2.

Previous: A dictionary comprehension maps each number to its square. What is printed? · Next: Which of these expressions creates a generator object rather than a fully…

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