- How to update a Python dictionary values?
- Example
- Output
- Dictionary Update Using The Update Method
- Example
- Output
- Dictionary Update Using The Square Brackets
- Example
- Output
- Swap dictionary keys and values in Python
- Swap keys and values with dictionary comprehension and items()
- Note on dictionaries with duplicate values
- Change Dictionary Values in Python
- Change Dictionary Values in Python Using the dict.update() Method
- Change Dictionary Values in Python Using the for Loop
- Change Dictionary Values in Python by Unpacking Dictionary Using the * Operator
- Related Article — Python Dictionary
- Change Dictionary Values in Python
- Change Dictionary Values in Python Using the dict.update() Method
- Change Dictionary Values in Python Using the for Loop
- Change Dictionary Values in Python by Unpacking Dictionary Using the * Operator
- Related Article — Python Dictionary
How to update a Python dictionary values?
Values of a Python Dictionary can be updated using the following two ways i.e. using the update() method and also, using square brackets.
Dictionary represents the key-value pair in Python, enclosed in curly braces. The keys are unique and a colon separates it from value, whereas comma separates the items. With that, the left size before the colon are keys, whereas right its corresponding values.
Let us first create a Python Dictionary and fetch all the values. Here, we have included 4 key-value pairs in the Dictionary and displayed them. Product, Model, Units, and Available are keys of the Dictionary. Except the Units key, all are having String values −
Example
# Creating a Dictionary with 4 key-value pairs myprod = "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" > # Displaying the Dictionary print(myprod) # Displaying individual values print("Product token punctuation">,myprod["Product"]) print("Model token punctuation">,myprod["Model"]) print("Units token punctuation">,myprod["Units"]) print("Available token punctuation">,myprod["Available"])
Output
Product = Mobile Model = XUT Units = 120 Available = Yes
Above, we have displayed the 4-key-value pairs in a Dictionary with Product Information. Now, we will see the two ways to update Dictionary values in Python.
Dictionary Update Using The Update Method
Let us now update the Dictionary values using the update() method. We have first displayed the Dictionary before updating the values. After that, the update() is used and the updated values are placed as a parameter of the method. Here, we have updated only two key values i.e. Product and Model −
Example
# Creating a Dictionary with 4 key-value pairs myprod = "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" > # Displaying the Dictionary print("Dictionary = \n",myprod) print("Product token punctuation">,myprod["Product"]) print("Model token punctuation">,myprod["Model"]) # Updating Dictionary Values myprod.update("Product":"SmartTV","Model": "PHRG6",>) # Displaying the Updated Dictionary print("\nUpdated Dictionary = \n",myprod) print("Updated Product token punctuation">,myprod["Product"]) print("Updated Model token punctuation">,myprod["Model"])
Output
Dictionary = Product = Mobile Model = XUT Updated Dictionary = Updated Product = SmartTV Updated Model = PHRG6
In the output, we can see the 1st two values updated using the updated() method, rest remained the same.
Dictionary Update Using The Square Brackets
Here is another code. Let us now update the Dictionary values without using the update() method. We will use the square brackets to update individual values. Here, we have updated only two key values i.e. Units and Available. The square brackets have the corresponding keys for the values to be updated −
Example
# Creating a Dictionary with 4 key-value pairs myprod = "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" > # Displaying the Dictionary print("Dictionary = \n",myprod) print("Product token punctuation">,myprod["Product"]) print("Model token punctuation">,myprod["Model"]) # Updating Dictionary Values myprod["Units"] = 170 myprod["Available"] = "No" # Displaying the Updated Dictionary print("\nUpdated Dictionary = \n",myprod) print("Updated Units token punctuation">,myprod["Units"]) print("Updated Availability token punctuation">,myprod["Available"])
Output
Dictionary = Product = Mobile Model = XUT Updated Dictionary = Updated Units = 170 Updated Availability = No
In the output, we can see the last two values updated without using the updated() method, rest remained the same.
Swap dictionary keys and values in Python
This article explains how to swap keys and values in a dictionary ( dict ) in Python.
Swap keys and values with dictionary comprehension and items()
You can swap keys and values in a dictionary with dictionary comprehensions and the items() method.
d = 'key1': 'val1', 'key2': 'val2', 'key3': 'val3'> d_swap = v: k for k, v in d.items()> print(d_swap) #
def get_swap_dict(d): return v: k for k, v in d.items()> d_swap = get_swap_dict(d) print(d_swap) #
Note on dictionaries with duplicate values
In a dictionary, all keys must be unique, but values can be duplicated.
When swapping keys and values in a dictionary that contains duplicate values, only one instance of the duplicate value will be retained, since dictionary keys must be unique.
d_duplicate = 'key1': 'val1', 'key2': 'val1', 'key3': 'val3'> d_duplicate_swap = get_swap_dict(d_duplicate) print(d_duplicate_swap) #
Change Dictionary Values in Python
- Change Dictionary Values in Python Using the dict.update() Method
- Change Dictionary Values in Python Using the for Loop
- Change Dictionary Values in Python by Unpacking Dictionary Using the * Operator
This tutorial will look into multiple ways of changing the specific key’s value in the Python dictionary. We can do it by using the below methods,
Change Dictionary Values in Python Using the dict.update() Method
In this method, we pass the new key-value pairs to the update() method of the dictionary object. We can change one and more key-value pairs using the dict.update() method.
my_dict = < 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> print('Original:') print(my_dict) my_dict.update('Khan': 6, 'Luna': 9>) print('\nAfter update:') print(my_dict)
Original: 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> After update: 'Khan': 6, 'Ali': 2, 'Luna': 9, 'Mark': 11, 'Pooja': 8, 'Sara': 1>
Change Dictionary Values in Python Using the for Loop
In this method, we keep iterating through the dictionary using the for loop until we find the key whose value needs to be modified. After getting the key, we can change the key’s value by assigning a new value to it.
my_dict = < 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> for key, value in my_dict.items(): if key == 'Ali': my_dictPython dictionary change key and value = 10 print(my_dict)
'Khan': 4, 'Ali': 10, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1>
Change Dictionary Values in Python by Unpacking Dictionary Using the * Operator
In this method, we can change the dictionary values by unpacking the dictionary using the * operator and then adding the one or more key-value pairs we want to change the dictionary.
my_dict = < 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> my_dict = < **my_dict, 'Pooja': 12> print(my_dict)
Related Article — Python Dictionary
Copyright © 2023. All right reserved
Change Dictionary Values in Python
- Change Dictionary Values in Python Using the dict.update() Method
- Change Dictionary Values in Python Using the for Loop
- Change Dictionary Values in Python by Unpacking Dictionary Using the * Operator
This tutorial will look into multiple ways of changing the specific key’s value in the Python dictionary. We can do it by using the below methods,
Change Dictionary Values in Python Using the dict.update() Method
In this method, we pass the new key-value pairs to the update() method of the dictionary object. We can change one and more key-value pairs using the dict.update() method.
my_dict = < 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> print('Original:') print(my_dict) my_dict.update('Khan': 6, 'Luna': 9>) print('\nAfter update:') print(my_dict)
Original: 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> After update: 'Khan': 6, 'Ali': 2, 'Luna': 9, 'Mark': 11, 'Pooja': 8, 'Sara': 1>
Change Dictionary Values in Python Using the for Loop
In this method, we keep iterating through the dictionary using the for loop until we find the key whose value needs to be modified. After getting the key, we can change the key’s value by assigning a new value to it.
my_dict = < 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> for key, value in my_dict.items(): if key == 'Ali': my_dictPython dictionary change key and value = 10 print(my_dict)
'Khan': 4, 'Ali': 10, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1>
Change Dictionary Values in Python by Unpacking Dictionary Using the * Operator
In this method, we can change the dictionary values by unpacking the dictionary using the * operator and then adding the one or more key-value pairs we want to change the dictionary.
my_dict = < 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1> my_dict = < **my_dict, 'Pooja': 12> print(my_dict)
Related Article — Python Dictionary
Copyright © 2023. All right reserved