ProgrammingHardAccenture pattern
What is printed?
What is printed?
import itertools print(list(itertools.chain([1, 2], [3, 4]))) print(list(itertools.chain.from_iterable([[1, 2], [3, 4]])))
- A. [[1, 2], [3, 4]] [1, 2, 3, 4]
- B. [1, 2, 3, 4] [1, 2, 3, 4]
- C. [1, 2, 3, 4] [[1, 2], [3, 4]]
- D. [1, 2, [3, 4]] [1, 2, 3, 4]
Correct answer: B. [1, 2, 3, 4] [1, 2, 3, 4]
chain concatenates the given iterables end to end, giving [1,2,3,4]. chain.from_iterable does the same but takes a single iterable OF iterables as input, here also flattening to [1,2,3,4]. Both produce the identical flat result here.
More Python Programming questions
- What is printed? python import itertools print(list(itertools.combinations([1, 2, 3, 4], 2)))
- What is printed? python import itertools print(list(itertools.accumulate([1, 2, 3, 4]))) print(list(itertools.accumulate([1, 2, 3, 4]…
- What is printed? python print(list(zip([1, 2, 3], [4, 5], [6, 7, 8, 9]))) from itertools import ziplongest print(list(ziplongest([1, 2…
- What is printed? python print(list(enumerate(["a", "b", "c"], start=5)))
- What is printed? python from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass try: s = Shape() except…
- What is printed? python class Meta(type): def new(mcs, name, bases, ns): print("creating", name) return super().new(mcs, name, bases, ns)…