How to Reverse a Dictionary in Python

To reverse a dictionary in Python, swap each key and value with a dictionary comprehension: {v: k for k, v in d.items()}. That works when the values are unique and hashable. If you want the same pairs in reverse order instead, use dict(reversed(d.items())):

codes = {"TX": "Texas", "CA": "California"}
{v: k for k, v in codes.items()}   # {'Texas': 'TX', 'California': 'CA'}

Every output below came from real runs on Python 3.12.5, using state codes, product prices in USD and 2026 order dates.

I’ll cover the swap itself, what happens with duplicate values, reversing the order, and values like lists that can’t become keys.

Reverse a dictionary by swapping keys and values

The comprehension reads each (key, value) pair and writes it back the other way round. If comprehensions are new to you, my guide to Python dictionary comprehension explains the syntax:

state_codes = {"TX": "Texas", "CA": "California", "NY": "New York", "FL": "Florida"}

by_name = {name: code for code, name in state_codes.items()}
print(by_name)
print(by_name["California"])

# Two other ways to get the same result
print(dict(zip(state_codes.values(), state_codes.keys())) == by_name)
print(dict(map(reversed, state_codes.items())) == by_name)

Output:

{'Texas': 'TX', 'California': 'CA', 'New York': 'NY', 'Florida': 'FL'}
CA
True
True
Command Prompt output of a Python dict comprehension swapping US state codes and state names
The swapped dictionary maps each state name to its code, and both alternatives build an equal dictionary.

zip() pairs the values with the keys in the same order, so it gives an identical result; the Python zip() function guide shows how that pairing works. I still reach for the comprehension because it’s the easiest to read.

What happens when a dictionary has duplicate values?

Keys must be unique, so when two keys share a value, the later one silently overwrites the earlier one. Nothing warns you. Here two products both cost $19.99:

from collections import defaultdict

prices = {"SKU-101": 19.99, "SKU-102": 24.50, "SKU-103": 19.99, "SKU-104": 9.75}

flipped = {price: sku for sku, price in prices.items()}
print(flipped)                                  # SKU-101 is gone
print(len(prices), "->", len(flipped))

grouped = defaultdict(list)
for sku, price in prices.items():
    grouped[price].append(sku)
for price, skus in grouped.items():
    print(f"${price:.2f}: {skus}")

Output:

{19.99: 'SKU-103', 24.5: 'SKU-102', 9.75: 'SKU-104'}
4 -> 3
$19.99: ['SKU-101', 'SKU-103']
$24.50: ['SKU-102']
$9.75: ['SKU-104']
Command Prompt output of a Python dictionary with duplicate prices losing a SKU when inverted, then grouped with defaultdict
The plain swap keeps only SKU-103 for $19.99 and drops from 4 entries to 3; the grouped version keeps both SKUs.

Comparing len() before and after is a quick way to catch the loss. To group instead, collect keys in a list with defaultdict(list). You can also remove duplicate values from a Python dictionary first if only one key should win.

Reverse the order of a dictionary in Python

Sometimes “reverse” means the order, not the mapping. Dictionaries keep insertion order, and since Python 3.8 reversed() works on a dict and its views:

orders = {"2026-10-01": 42.00, "2026-10-02": 18.25, "2026-10-03": 76.10}

print(list(reversed(orders)))                   # keys, newest first
newest_first = dict(reversed(orders.items()))
print(newest_first)
print(dict(reversed(list(orders.items()))))     # works on any version

Output:

['2026-10-03', '2026-10-02', '2026-10-01']
{'2026-10-03': 76.1, '2026-10-02': 18.25, '2026-10-01': 42.0}
{'2026-10-03': 76.1, '2026-10-02': 18.25, '2026-10-01': 42.0}
Command Prompt output of Python reversed() on a dictionary of 2026 order dates, printing keys and items newest first
reversed() lists the dates newest first, and dict(reversed(d.items())) builds a new dictionary in that order.

The list() version also runs on older Pythons. If you need a specific order rather than the reverse of insertion, sorting a Python dictionary with sorted(..., reverse=True) is the better tool.

Invert a dictionary with list values

Only hashable objects can be keys, so a value that’s a list raises TypeError. Convert it to a tuple, or give each item in the list its own entry:

teams = {"Emily": ["Python", "SQL"], "James": ["Excel"], "Olivia": ["Python"]}

try:
    flipped = {skills: name for name, skills in teams.items()}
except TypeError as err:
    print("TypeError:", err)

as_tuples = {tuple(skills): name for name, skills in teams.items()}
print(as_tuples)

by_skill = {}
for name, skills in teams.items():
    for skill in skills:
        by_skill.setdefault(skill, []).append(name)
print(by_skill)

Output:

TypeError: unhashable type: 'list'
{('Python', 'SQL'): 'Emily', ('Excel',): 'James', ('Python',): 'Olivia'}
{'Python': ['Emily', 'Olivia'], 'SQL': ['Emily'], 'Excel': ['James']}
Command Prompt output of Python raising TypeError unhashable list when inverting a dictionary, then using tuples and setdefault
Lists can’t be keys, tuples can, and the per-skill dictionary lists everyone who has each skill.

The per-item version is usually what you want: it answers “who knows Python?” directly. For reading values from a dict like this, see a Python dictionary with multiple values.

Do you need a reversed dictionary or just a reverse lookup?

If you only look up one value once, building a whole new dictionary is overkill. A generator like next(k for k, v in d.items() if v == target) is enough, and finding a key by value in a Python dictionary covers that approach.

Build the inverted dictionary when you’ll look up by value many times. Each lookup is then a constant-time key access instead of a scan.

Ways to reverse a dictionary compared

GoalCodeDuplicate values
Swap keys and values{v: k for k, v in d.items()}Last key wins
Swap with zip()dict(zip(d.values(), d.keys()))Last key wins
Group keys by valuedefaultdict(list) loopAll keys kept
Reverse the orderdict(reversed(d.items()))Not affected

Want more dictionary practice? Try these next:

Frequently asked questions

How do I reverse a dictionary in Python?

Swap the pairs with a comprehension: {v: k for k, v in d.items()}. The values must be unique and hashable.

How do I swap keys and values without losing duplicates?

Group keys in lists: loop over d.items() and call inv.setdefault(v, []).append(k), or use defaultdict(list).

How do I reverse the order of a dictionary?

Use dict(reversed(d.items())) on Python 3.8 or later, or dict(reversed(list(d.items()))) on any version.

Why do I get TypeError: unhashable type: ‘list’?

A list value can’t become a key. Convert it with tuple() or add one entry per list item.

Is the dict comprehension faster than zip()?

Both are linear and fast for normal sizes. Pick the comprehension for readability.

Where is reversed() on dictionaries documented?

See the dict mapping type in the Python docs.