Code Logo

Library Card Pairing with zip()

Published at22 Apr 2026
Python List and Dictionary Easy 0 views
Like0

A school librarian has prepared two matching lists for a new student registration day. One list contains student names, and the second list contains the library card numbers that belong to those same students in the same order.

Your task is to turn those two lists into a single dictionary. Each student name should become a key, and the matching card number should become the value stored for that key.

For example, if the input lists are ["Ayu", "Bima", "Caca"] and ["LC101", "LC102", "LC103"], the result should be {"Ayu": "LC101", "Bima": "LC102", "Caca": "LC103"}. If both lists are empty, the result should be an empty dictionary.

You can assume the two input lists always have the same length. The main goal is to connect values from one list with the values at the same positions in the other list and return the final mapping.

Example Input & Output

Example 1
Input
names = ["Ayu", "Bima", "Caca"], card_numbers = ["LC101", "LC102", "LC103"]
Output
{"Ayu": "LC101", "Bima": "LC102", "Caca": "LC103"}
Explanation

Each student name is paired with the card number in the same position.

Example 2
Input
names = [], card_numbers = []
Output
{}
Explanation

Two empty lists produce an empty directory.

Example 3
Input
names = ["Dina"], card_numbers = ["LC777"]
Output
{"Dina": "LC777"}
Explanation

A one-item pair of lists becomes a one-item dictionary.

Algorithm Flow

Recommendation Algorithm Flow for Library Card Pairing with zip()
Recommendation Algorithm Flow for Library Card Pairing with zip()

Solution Approach

This challenge is really about pairing two ordered lists together. The first element in names belongs with the first element in card_numbers, the second belongs with the second, and so on.

Python has a built-in tool that fits that idea perfectly: zip(). It walks through two sequences at the same time and produces pairs of matching values. Once those pairs exist, building the dictionary becomes very direct.

The shortest version is:

return dict(zip(names, card_numbers))

That line asks Python to pair every name with the card number at the same position, then convert the resulting pairs into a dictionary. It is compact, but the logic stays very readable because the input structure already matches what zip() expects.

If you prefer to see the steps more clearly, you can also loop through the paired values and assign them one by one:

directory = {}
for name, card in zip(names, card_numbers):
    directory[name] = card
return directory

Both versions do the same job. The important idea is that zip() keeps the two lists aligned without needing a manual index variable. That makes it a very natural Python solution for problems where two lists describe one shared set of records.

The runtime is O(n) because each pair is processed once, and the output dictionary stores one entry per student. For a beginner problem about moving from lists into dictionary data, this is a clean pattern to learn early.

Best Answers

python - Approach 1
def build_card_directory(names, card_numbers):
    return dict(zip(names, card_numbers))