October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Use `zip()` to Build a Dictionary from Two Python Lists

Use `dict(zip(keys, values))` to pair items by position and create a Python dictionary. Choose default truncation, strict length checking, or padding to match your data.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pair corresponding items in the two lists with zip(), then pass those pairs to dict():

keys = ["name", "age"]
values = ["Ada", 36]

result = dict(zip(keys, values))
print(result)  # {'name': 'Ada', 36}

How the conversion works

zip(keys, values) pairs items by position: the first key with the first value, the second key with the second value, and so on. The resulting pairs are passed to dict(), which creates a dictionary from an iterable of key-value pairs.

As an Amazon Associate I earn from qualifying purchases.

For example:

keys = ["color", "size", "in_stock"]
values = ["blue", "medium", True]

product = dict(zip(keys, values))
# {'color': 'blue', 'size': 'medium', 'in_stock': True}

This uses Python’s built-in zip() and dict() functions; no import is needed. The Python documentation describes zip() and dictionary construction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What happens when the lists have different lengths?

By default, zip() stops as soon as the shorter iterable runs out. Any extra items in the longer list are ignored, so the resulting dictionary may omit data without raising an error.

Choose the behavior that matches your data:

Situation Approach Result
The lists should have equal lengths dict(zip(keys, values, strict=True)) On Python 3.10 and later, raises ValueError if their lengths differ. The strict parameter was added in Python 3.10.
Unequal lengths are acceptable, and unmatched items should be omitted dict(zip(keys, values)) Pairs items up to the end of the shorter input.
Unequal lengths are acceptable, and missing entries should be padded itertools.zip_longest() Pairs through the end of the longer input, filling missing items with a value you specify; the default fill value is None.

For example, to pad unmatched positions with None:

from itertools import zip_longest

keys = ["name", "age", "city"]
values = ["Ada", 36]

result = dict(zip_longest(keys, values))
# {'name': 'Ada', 'age': 36, 'city': None}

Choose padding carefully: if the shorter list supplies keys, a padded None key can become an actual dictionary key.

What if a key appears more than once?

Dictionary keys must be unique in the result. If the input key list contains duplicates, each later pair replaces the value from the earlier pair for that key:

keys = ["color", "color"]
values = ["blue", "green"]

result = dict(zip(keys, values))
# {'color': 'green'}
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use a dictionary comprehension

Use a comprehension when each paired value needs a transformation or when you want to filter pairs. It follows the same positional pairing and default shortest-input behavior as zip():

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
keys = ["name", "score", "level"]
values = ["Ada", 95, 4]

result = {
    key: value
    for key, value in zip(keys, values)
    if key != "level"
}
# {'name': 'Ada', 'score': 95}

If no transformation or filtering is needed, dict(zip(keys, values)) is the direct option.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.