Projects/mat_flat.py

19 lines
498 B
Python

from typing import List
"""
Flattens a 2D list (matrix) into a 1D list.
Args:
M (List[List]): A two-dimensional list to be flattened.
Returns:
List: A one-dimensional list containing all elements of the input matrix in row-major order.
Example:
>>> flatten([[1, 2], [3, 4]])
[1, 2, 3, 4]
NOTE: If you add the mat_flat module to a project, call it with mat_flat.flatten(M).
"""
def flatten(M: List[List])-> List:
flat = [elem for row in M for elem in row]
return flat