Answer key · 12 questions

Python itertools Exercises With Answers

All 12 questions for Python itertools 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 itertools, 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 count(5) produce?

  1. An infinite sequence starting at 5correct
  2. A list from 0 to 5
  3. A dictionary with five keys
  4. Exactly five values

Answer: An infinite sequence starting at 5

Why: count is lazy and infinite; islice or zip is needed to bound it.

02 · Multiple choice

What does chain do?

  1. Groups adjacent keys
  2. Joins iterables end to endcorrect
  3. Sorts values
  4. Counts values

Answer: Joins iterables end to end

Why: chain reads one iterable after another as a single stream.

03 · Multiple choice

What does list(islice(count(0), 3)) produce?

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

Answer: [0, 1, 2]

Why: islice takes the first three values from the infinite count stream.

04 · Multiple choice

What must you do before groupby if equal keys are scattered?

  1. Sort the datacorrect
  2. Call list twice
  3. Convert it to a string
  4. Use count

Answer: Sort the data

Why: groupby only groups adjacent items, so sort first when equal keys are not consecutive.

05 · Fill in the blank

Complete the function that joins two iterables.

from itertools import chain
print(list(____([1], [2])))

Answer: chain

Why: chain reads the first iterable, then the second.

06 · Fill in the blank

Complete the function that bounds the infinite count.

from itertools import count, islice
print(list(____(count(1), 2)))

Answer: islice

Why: islice takes a fixed number of values from an iterable.

07 · Fill in the blank

Complete the function that groups adjacent values.

from itertools import groupby
data = [1, 1, 2]
for key, group in ____(data):
    print(key, len(list(group)))

Answer: groupby

Why: groupby groups consecutive items that share a key.

08 · Fill in the blank

Complete the import for joining two lists.

from itertools import ____
print(list(chain([1], [2])))

Answer: chain

Why: The chain function is imported from itertools.

09 · Predict output

What does the program output?

from itertools import count, islice
print(list(islice(count(3), 2)))

Answer: [3, 4]

Why: The stream starts at three, and islice takes two values.

10 · Predict output

What appears on the screen?

from itertools import chain
print(list(chain("ab", "cd")))

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

Why: chain joins the two strings into one stream of characters.

11 · Predict output

What is printed?

from itertools import chain
print(list(chain([1], [2, 3])))

Answer: [1, 2, 3]

Why: chain reads the first list and then the second list.

12 · Predict output

What is the output?

from itertools import groupby
data = [1, 1, 2]
print([(k, len(list(g))) for k, g in groupby(data)])

Answer: [(1, 2), (2, 1)]

Why: The first group has two ones and the second group has one two.

Try the questions first

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

Practice this lesson