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

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']

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}

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']}

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
| Goal | Code | Duplicate 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 value | defaultdict(list) loop | All keys kept |
| Reverse the order | dict(reversed(d.items())) | Not affected |
Want more dictionary practice? Try these next:
- Iterate through a dictionary in Python
- Convert a dictionary to a list in Python
- Get the keys of a dictionary in Python
- Reverse a list in Python
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.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn