Skip to content

8. Zip Columns into Records

Difficulty: Easy · Topic: Core Python & data structures

An export arrives as three parallel lists: ids, names and cities, where position i of each list belongs to the same rider. Write to_records(ids, names, cities): return a list of dicts with the keys 'id', 'name' and 'city', one per position, in order.

Example: to_records([1, 2], ['asha', 'ben'], ['Pune', 'Goa']) gives [{'id': 1, 'name': 'asha', 'city': 'Pune'}, {'id': 2, 'name': 'ben', 'city': 'Goa'}].

Your code runs in CPython on our servers against 6 tests, including hidden ones.

Sample test

to_records([1, 2], ['asha', 'ben'], ['Pune', 'Goa'])

Expected: [{'id': 1, 'name': 'asha', 'city': 'Pune'}, {'id': 2, 'name': 'ben', 'city': 'Goa'}]

Hints

Hint 1zip(a, b, c) walks the three lists side by side.
Hint 2Plain zip stops at the shortest list without telling you. One keyword argument makes it raise instead.

Learn the concepts

More Python problems

All Python problems