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 eachrow
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]
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.