Answer key · 12 questions

Python List Comprehensions Exercises With Answers

All 12 questions for Python List Comprehensions in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python List Comprehensions, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does [n * 2 for n in range(3)] produce?

  1. [0, 2, 4]correct
  2. [0, 1, 2]
  3. [2, 4, 6]
  4. [1, 2, 3]

Answer: [0, 2, 4]

Why: range(3) yields 0, 1, 2; each value is multiplied by 2.

02 · Multiple choice

Which clause filters values in a comprehension?

  1. ifcorrect
  2. return
  3. for
  4. while

Answer: if

Why: The if clause keeps only values that satisfy the condition.

03 · Multiple choice

Which syntax builds a dictionary comprehension?

  1. {k, v for k, v in items}
  2. [k: v for k, v in items]
  3. {k: v for k, v in items}correct
  4. (k, v for k, v in items)

Answer: {k: v for k, v in items}

Why: A dictionary comprehension uses braces and a key: value expression.

04 · Multiple choice

What does [x for x in "abc"] produce?

  1. [abc]
  2. ['abc']
  3. "abc"
  4. ['a', 'b', 'c']correct

Answer: ['a', 'b', 'c']

Why: Iterating over a string yields one-character strings.

05 · Fill in the blank

Complete the operator that squares each number.

squares = [n ____ n for n in range(4)]
print(squares)

Answer: *

Why: n * n multiplies each value by itself.

06 · Fill in the blank

Complete the clause that keeps only even values.

nums = [1, 2, 3, 4]
evens = [n for n in nums ____ n % 2 == 0]
print(evens)

Answer: if

Why: The if clause filters the values produced by the for clause.

07 · Fill in the blank

Complete the function that provides each word's length.

words = ["a", "bb"]
lengths = {word: ____(word) for word in words}
print(lengths)

Answer: len

Why: len(word) returns the number of characters in the word.

08 · Fill in the blank

Complete the nested loop keyword.

pairs = [(x, y) ____ x in [1, 2] for y in [3, 4]]
print(pairs)

Answer: for

Why: The second for clause produces the inner values.

09 · Predict output

What does this code print?

print([n + 1 for n in [1, 2, 3]])

Answer: [2, 3, 4]

Why: Each value is increased by one.

10 · Predict output

What does the program output?

print([n for n in range(6) if n % 2 == 0])

Answer: [0, 2, 4]

Why: The comprehension keeps 0, 2, and 4.

11 · Predict output

What appears on the screen?

print({n: n * n for n in range(3)})

Answer: {0: 0, 1: 1, 2: 4}

Why: The dictionary maps 0, 1, and 2 to their squares.

12 · Predict output

What is printed?

print([len(w) for w in ["a", "bb", "ccc"]])

Answer: [1, 2, 3]

Why: The lengths are 1, 2, and 3.

Try the questions first

Open the practice page for Python List Comprehensions and answer the same 12 questions with instant feedback.

Practice this lesson