Flatten a List of Lists
Completed
Read n, then n lines of integers. Print all numbers in one flat list using a nested comprehension.
Input
Line 1: n. Then n lines.
Output
The flat list.
Example
Input
3 1 2 3 4 5 6
Output
[1, 2, 3, 4, 5, 6]
Editor
Python
Hints
The loops in a comprehension read in the same order as nested for statements.
Solution explanation
[x for row in rows for x in row] is the same as an outer loop over rows and an inner loop over x.
Show solution
n = int(input())
rows = [list(map(int, input().split())) for _ in range(n)]
print([x for row in rows for x in row])