itertools & partial

Preview — 3 of 10 questions

What is the output of the following code?

javascript
from itertools import chain

a = [1, 2]
b = (3, 4)
c = [5, 6]

result = list(chain(a, b, c))
print(result)
ATypeError: chain() takes exactly one argument
B[[1, 2], (3, 4), [5, 6]]
C[1, 2, 3, 4, 5, 6, [1, 2], (3, 4), [5, 6]]
D[1, 2, 3, 4, 5, 6]

What is the output of the following code?

javascript
from itertools import islice

def counter():
    n = 0
    while True:
        yield n
        n += 1

first_five = list(islice(counter(), 5))
print(first_five)

skip_two_take_three = list(islice(counter(), 2, 5))
print(skip_two_take_three)
A[0, 1, 2, 3, 4] then [0, 1, 2]
B[0, 1, 2, 3, 4] then [2, 3, 4]
CTypeError: 'generator' object is not subscriptable
D[1, 2, 3, 4, 5] then [2, 3, 4]

What is the output of the following code?

javascript
from itertools import product

colors = ["red", "blue"]
sizes = ["S", "M"]

combos = list(product(colors, sizes))
print(combos)
print(len(combos))
A[('red', 'S'), ('red', 'M'), ('blue', 'S'), ('blue', 'M')] then 4
B[('red', 'blue'), ('S', 'M')] then 2
CTypeError
D[('red', 'S'), ('blue', 'M')] then 2

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.