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
elementfrom eachrowand 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]
Previous lessonCoding Quiz - Flattening a 2D ArrayNext lessonCoding Quiz - Calculate the Sum of a List Using Divide and Conquer
Lessons in this chapter · Practice with Common Coding Quiz Types
- 1. Coding Quiz - Finding Rotten Apples
- 2. Finding Rotten Apples Explained
- 3. Coding Quiz - Calculating the Sum of a List Recursively
- 4. Explanation for Recursively Calculating the Sum of a List
- 5. Coding Quiz - 1, 2, 3 Addition
- 6. Explanation for Adding 1, 2, 3
- 7. Coding Quiz - Flattening a 2D Array
- 8. Converting to a 1D Array Explanation
- 9. Coding Quiz - Calculate the Sum of a List Using Divide and Conquer
- 10. Explanation of Calculating the Sum of a List Using Divide and Conquer
- 11. Coding Quiz - Gym Uniform Problem
- 12. Gym Uniform Problem Explanation
- 13. Coding Quiz - List of Integers with Sum Greater than Target
- 14. Explanation of Integer List with Sum Greater Than Target
- 15. Coding Quiz - Finding Travel Routes
- 16. Finding Travel Itinerary Explanation
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help