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 my_dict.items() to loop through the key–value pairs already in a dictionary. Use dict(zip(keys, values)) to build a dictionary from separate, positionally aligned iterables. Passing a dictionary directly to zip() pairs its keys, not its key–value pairs.

Loop through a dictionary’s existing key–value pairs

When both keys and values are already stored in one dictionary, .items() is the direct way to retrieve them together:

my_dict = {"name": "Lancelot", "quest": "the Holy Grail"}

for key, value in my_dict.items():
    print(key, value)

Each iteration produces a key–value pair, which Python unpacks into key and value. You do not need zip() for this job. The Python 3.13.15 data-structures tutorial uses .items() to retrieve a dictionary’s keys and corresponding values.

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

Create a dictionary from separate keys and values

If keys and values are in separate iterables in matching order, zip them and pass the pairs to dict():

keys = ["name", "quest", "favorite color"]
values = ["Lancelot", "the Holy Grail", "blue"]

answers = dict(zip(keys, values))
print(answers)
# {'name': 'Lancelot', 'quest': 'the Holy Grail', 'favorite color': 'blue'}

zip() pairs elements by position; it does not match items by their labels or infer a relationship from their contents. The first key is paired with the first value, the second key with the second value, and so on.

Choose how to handle unequal lengths

Ordinary zip() stops when its shortest input runs out. If one iterable has extra elements, those elements are ignored. Choose the behavior that matches your data:

  • Allow truncation: Use dict(zip(keys, values)) when ignoring unmatched trailing items is intended.
  • Require equal lengths: Use dict(zip(keys, values, strict=True)). It raises ValueError if the iterables have different lengths. The strict parameter is available in Python 3.10 and later.
  • Pad the shorter input: Use itertools.zip_longest() when you want iteration to continue through the longer input, supplying a fill value for missing elements.

These behaviors are documented in the Python 3.14.7 built-in functions reference.

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

Zip two dictionaries by position

To pair the first entry of one dictionary with the first entry of another, zip their .items() views:

dict1 = {"a": 1, "b": 2}
dict2 = {"x": 10, "y": 20}

for (key1, value1), (key2, value2) in zip(dict1.items(), dict2.items()):
    print(key1, value1, key2, value2)

This is positional pairing, not a lookup by equal key. If the goal is to compare values for matching keys, use the keys to express that relationship explicitly rather than assuming entries at the same position correspond. Also note that zip(dict1, dict2) pairs the dictionaries’ keys: iterating over a dictionary yields its keys.

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

Reverse a dictionary when its values are unique

To make the original values the new keys and the original keys the new values, use:

original = {"a": 1, "b": 2}
reversed_dict = dict(zip(original.values(), original.keys()))
print(reversed_dict)
# {1: 'a', 2: 'b'}

Dictionary keys and values are iterated in insertion order, so the two views stay aligned when zipped. This reversal works as a one-to-one mapping only when the original values are unique and can be used as dictionary keys. If values repeat, the new dictionary cannot retain every original association under the same key; a later pair overwrites an earlier one. To preserve all associations, group the original keys into lists by value instead.

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

For the relevant iteration and dictionary-view behavior, see the Python 3.14.7 built-in types reference.

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.