In this post, we are going to learn how to swap key-value in Python Dictionary. Dictionary is an unordered collection of data in form of key-value, there is a one-to-one mapping between key-values pairs. Each key has one value. The key in the dictionary is unique but values can be duplicated along with a key. Sometimes many keys have duplicate values. So we will learn what will happen if we swap keys that have duplicate values.
1. Using for-loop to swap key-value in Python Dictionary
In this code example, we are using the for-loop to loop over the elements of the dictionary and we assign an original key to the value of the new dictionary on each iteration to swap the key to value.
Code example
#Python Program to swap key-value in Python Dictionary
langdict = {'C#':1, 'C':5, 'Java':6,'Python':10,'Rust':60}
#swapping the keys and values
swappeddict={}
for key, value in langdict.items():
swappeddict[value] = key
print('swapped dictionary is =\n',swappeddict)
Output
swapped dictionary is =
{1: 'C#', 5: 'C', 6: 'Java', 10: 'Python', 60: 'Rust'}
2. Dictionary comprehension to swap key-value in Python Dictionary
In this code example, we are using dictionary comprehension to swap keys and values, Dictionary comprehension provides an easy and shorter code.
Code example
#Python Program to swap key-value in Python Dictionary
langdict = {'C#':1, 'C':5, 'Java':6,'Python':10,'Rust':60}
#swapping the keys and values
swappeddict = dict((value,key) for key,value in langdict.items())
print('swapped dictionary is =\n',swappeddict)
Output
swapped dictionary is =
{1: 'C#', 5: 'C', 6: 'Java', 10: 'Python', 60: 'Rust'}
3. How to Swap duplicate values with keys using Dictionary Comprehension
Here we are going to understand a case where we have duplicate values in the dictionary. If the dictionary has two keys that have the same value, What will happen when we will swap the key-value pair. Let us understand this example.
In the below code, ‘C’ and ‘Rust’ both keys have the same values. While swapping the key-value pair, it considers the first key-value that appears first and removes the later kay-value pair, As we can see the {‘Rust’:5} is removed while swapping.
Code example
#Python Program to swap key-value in Python Dictionary
langdict = {'C#':1, 'C':5, 'Java':6,'Python':10,'Rust':5}
#swapping the keys and values
swappeddict = dict((value,key) for key,value in langdict.items())
print('swapped dictionary is =\n',swappeddict)
Output
swapped dictionary is =
{1: 'C#', 5: 'Rust', 6: 'Java', 10: 'Python'}
4. Using zip() to swap key-value in Python Dictionary
The zip() method takes an iterable(tuple, list, dictionary) as an argument and combines them together as a tuple.In this code example, we are taking dictionary(langdict) keys, values as an iterable and combining them using dict() converting them in the dictionary, and return a new dictionary swappeddict.
Code example
#Python Program to swap key-value in Python Dictionary
langdict = {'C#':1, 'C':5, 'Java':6,'Python':10,'Rust':60}
#swapping the keys and values
swappeddict = dict(zip(langdict.values(), langdict.keys()))
print('swapped dictionary is =\n',swappeddict)
Output
swapped dictionary is =
{1: 'C#', 5: 'C', 6: 'Java', 10: 'Python', 60: 'Rust'}
5. How to swap key-value in Python Dictionary Zip() with duplicate values
If the dictionary has two keys that have the same value, What will happen when we will swap the key-value pair let us understand with this example.In the below code, ‘C’ and ‘Rust’ both keys have the same values. While swapping the key-value pair, it considers the first key-value that appears first and removes the later kay-value pair, As we can see the {‘Rust’:5} is removed while swapping.
#Python Program to swap key-value in Python Dictionary
langdict = {'C#':1, 'C':5, 'Java':6,'Python':10,'Rust':5}
#swapping dictionary keys and values
swappeddict = dict(zip(langdict.values(), langdict.keys()))
print('swapped dictionary is =\n',swappeddict)
Output
swapped dictionary is =
{1: 'C#', 5: 'Rust', 6: 'Java', 10: 'Python'}
Conclusion
In this post, We have understood three different ways to swap key-value in Python Dictionary. We have used different techniques like for loop, Dictionary Comprehension, zip() to swap key to value. Dictionary comprehension provides a shorter code over for loop. We hope you will use some of these techniques while working on data problems.Happy Learning!!