Iterators and generators · 10 / 15 Exercise Medium

Flatten with yield from

Objective

Create a Python program with a generator that joins several lists into a single sequence using yield from.

Instructions

  • Read an integer n and then n lines, each with integers separated by spaces (at least one per line). Build a list of lists.
  • Define def flatten(lists), which for each sublist yields all its elements with yield from sublist.
  • Store the values in a list with list(flatten(lists)).
  • Show the values on one line, separated by spaces, and then Items: K.
Input
3
1 2
3
4 5 6
Output
1 2 3 4 5 6
Items: 6

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise