Python Dictionary Convert Values to Keys Example
Hi Folks,
This is a short guide on python dictionary convert values to keys. We will look at an example of python dict change value to key. I would like to share with you how to change value of key in dictionary python. This article will give you a simple example of how to convert value of key in dictionary python.
There are several ways to convert python dictionary values to keys. i will give you simple example using for loop and items() function.
So, without further ado, let's see simple examples:
You can use these examples with python3 (Python 3) version.
Example:
main.py
# Created New Dictionary
myDictionary = {
"1": "One",
"2": "Two",
"3": "Three",
}
# Convert Dictionary Values to Keys
newDictionary = {y: x for x, y in myDictionary.items()}
print(newDictionary)
Output:
{'One': '1', 'Two': '2', 'Three': '3'}
I hope it can help you...