Lecture

Converting to a 1D Array Explanation

Write a function that converts a 2D array into a 1D array.

Use list comprehension to convert each element of the 2D array into a 1D array.

This can be implemented with concise and efficient one-liner code.


Sample Solution
def solution(matrix): return [element for row in matrix for element in row]
  • This uses list comprehension to sequentially extract element from each row and returns it as a new 1D array.

Example Usage

Input/Output Example
print(solution([[1, 2], [3, 4], [5]])) # Output: [1, 2, 3, 4, 5]

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help