easy +8 pts

Concatenate Horizontally

Merge two 2D lists side by side without external libraries.

Complete the function `concatenate_horizontally(arr1, arr2)` that takes two 2D lists (lists of lists) with the same number of rows and returns a new 2D list formed by placing each row of `arr2` to the right of the corresponding row of `arr1`. The result must be a new list; the inputs should not be modified. Rows may have different lengths, but the number of rows must be equal. If the second list is empty, return a copy of the first list; if the first list is empty, return a copy of the second list. You may not use NumPy or any external library.

Constraints

Input lists are 2D (lists of lists) with the same number of rows. Row counts are equal when both lists are non-empty. Lists can contain integers or floats. The function should handle any consistent number of rows and any number of columns (including zero columns, which means rows may be empty lists).

Example

>>> arr1 = [[1, 2], [3, 4]]
>>> arr2 = [[5], [6]]
>>> concatenate_horizontally(arr1, arr2)
[[1, 2, 5], [3, 4, 6]]
>>> arr1 = [[1, 2, 3]]
>>> arr2 = [[4, 5, 6]]
>>> concatenate_horizontally(arr1, arr2)
[[1, 2, 3, 4, 5, 6]]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Check if either list is empty and handle those cases first.
Use `zip` to pair up rows from both lists.
For each pair of rows, create a new list that is the concatenation of the two rows using `+` or `list.extend` on a copy.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.