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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use dict(zip(keys, values)) to make a dictionary from two lists whose items match by position. The first list supplies keys; the second supplies their values.

Build a dictionary from matching lists

Pair each key with the value at the same index, then pass those pairs to dict:

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

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

zip(keys, values) produces pairs in order: ("name", "Ada") and ("age", 36). The dict constructor accepts an iterable of two-item key-value pairs and creates the mapping. See the Python documentation for zip and dictionary construction.

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

Choose what happens when list lengths differ

By default, zip stops as soon as the shorter input is exhausted. Any remaining items in the longer list are ignored, so a length mismatch can silently omit data.

What the mismatch means Use Behavior
A mismatch is an error dict(zip(keys, values, strict=True)) Raises ValueError if the input lengths differ. The strict argument was added in Python 3.10.
Extra items should be ignored dict(zip(keys, values)) Pairs items until the shorter input ends.
Missing items should be filled itertools.zip_longest(keys, values) Pairs through the longer input and uses a fill value for missing items. Set an appropriate fillvalue if the default is not suitable.

For example, to make unequal lengths fail rather than quietly discard items, use strict mode:

result = dict(zip(keys, values, strict=True))

For padded pairs, choose the fill value explicitly and import zip_longest:

from itertools import zip_longest

result = dict(zip_longest(keys, values, fillvalue=None))

With fillvalue=None, an unmatched key receives None, or an unmatched value is paired with the key None. Those entries may not be appropriate for your data; select a fill value that makes sense for how the resulting dictionary will be used. The Python built-in functions documentation describes both strict pairing and itertools.zip_longest.

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

Transform or filter pairs with a comprehension

Use a dictionary comprehension when you need to change values or omit certain pairs. It uses the same positional pairing and default shortest-list behavior as zip:

result = {
    key: value.upper()
    for key, value in zip(keys, values)
    if isinstance(value, str)
}

This example uppercases string values and skips values that are not strings. Add your own condition or transformation as needed; use strict=True in the zip call if unequal lengths should raise an error.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Account for duplicate keys

A dictionary can contain each key only once. If the key list repeats a key, the later paired value replaces the earlier one. For example, dict(zip(["color", "color"], ["blue", "green"])) produces {"color": "green"}. If every occurrence must be preserved, a dictionary is not the right representation unless you store multiple values under each key.

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.

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