ProgrammingVery hardIBM · Deloitte · Product Company SDE
What does the following Python 3 code print? import itertools print(list(itertools.chain.from_iterable([[1, 2], [3], [4, 5, 6]])))
- A. [1, 2, 3, 4, 5, 6, 6]
- B. [1, 2, 3, 4, 5, 6]
- C. [[1, 2], [3], [4, 5, 6]]
- D. [1, 2, 3, 4, 5]
Correct answer: B. [1, 2, 3, 4, 5, 6]
chain.from_iterable flattens one level of nesting, concatenating all inner iterables in order.
More Python Programming questions
- What does the following Python 3 code print? import itertools print(list(itertools.product([1, 2], [3, 4])))
- What does the following Python 3 code print? import itertools print(list(itertools.combinationswithreplacement([1, 2, 3], 2)))
- What does the following Python 3 code print? import itertools print(list(itertools.compress([1, 2, 3, 4, 5], [1, 0, 1, 0, 1])))
- What does the following Python 3 code print? import itertools print(list(itertools.dropwhile(lambda x: x < 3, [1, 2, 3, 4, 1, 2])))
- What does the following Python 3 code print? import itertools print(list(itertools.takewhile(lambda x: x < 3, [1, 2, 3, 4, 1, 2])))
- What does the following Python 3 code print? class Base: def greet(self): return "base" class Mid(Base): def greet(self): return "mid-" +…