# 2-D List
matrix = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
# Nested List Comprehension to flatten a given 2-D matrix
flatten_matrix = [val for sublist in matrix for val in sublist]
print(flatten_matrix)
# This is the syntax of a general list comprehension expression in Python
[expression
for x1 in it1 if cond1
for x2 in it2 if cond2
...
for xn in itn if condn
]
# hereby it1,..., itn are iterables and cond1, ..., condn boolean conditions
# expression can depend on x1,...,xn and
# itj and condj can depend on the variables x1,...,xj-1 quantified before it
# The list comprehension expression is equivalent to the following nested
# for-loop statement
res = []
for x1 in it1:
if cond1:
for x2 in it2:
if cond2:
...
for xn in itn if condn:
if condn:
res.append(expression)