Project Description
Your task for today is to write a Python program that swaps the keys and values in a dictionary. That means turning something like {'a': 1, 'b': 2}
into {1: 'a', 2: 'b'}
.
This exercise is very useful in real-world Python work. For example, you might have a dictionary where usernames are keys and user IDs are values — and you want to reverse it to look up usernames by ID instead.
In this project, you'll be start by storing a sample dictionary in a variable. Here is your dictionary:
original = {'a': 1, 'b': 2, 'c': 3}
Your goal is to loop through it (or use a one-liner) to build a new dictionary where each value becomes a key, and each key becomes the value.
Expected Output
If the input dictionary is:
{'a': 1, 'b': 2, 'c': 3}
Your program should print:
💡 Hint
Click the Show Hint button below if you’re not sure how to get started. It will walk you through the first few lines and logic to build the new dictionary.
𝌣 Solution
Click the Show Solution button below to see both a loop-based and a dictionary-comprehension solution for this task.
🔒 This solution is available to paid subscribers only.
🚀 Want to keep leveling up?
Browse 200+ projects at dailypythonprojects.substack.com/archive and unlock all solutions by subscribing.
Build real Python skills daily and transform how you learn — one project at a time.
Reversed = { b:a for a,b in original.items() }