easy +8 pts

Zip Two Lists

Combine two lists into a list of pairs, handling different lengths gracefully.

Write a function `zip_lists(list_a, list_b)` that takes two lists and returns a new list of pairs. Each pair is a list `[a, b]` where `a` is from `list_a` and `b` is from `list_b` at the same index. The resulting list should be as long as the shorter input list. If either list is empty, return an empty list. Do not use the built-in `zip()` function; implement the logic yourself.

Constraints

Input lists can be of any length (including 0). Elements can be of any data type. The function should not modify the input lists. The time complexity should be O(min(n, m)) where n and m are the lengths of the inputs.

Example

>>> zip_lists([1, 2, 3], [4, 5, 6])
[[1, 4], [2, 5], [3, 6]]
>>> zip_lists([1, 2, 3], ['a', 'b'])
[[1, 'a'], [2, 'b']]
>>> zip_lists([], [1, 2])
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Find the length of the shorter list.
Loop over indices from 0 to that length minus one.
Append a list `[list_a[i], list_b[i]]` to the result.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.