Python Update Dictionary Value by Key
Last Updated :
08 Jul, 2024
A Dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and you can use various data types for both keys and values. Dictionaries are enclosed in curly braces {}, and the key-value pairs are separated by colons. Python dictionaries are mutable, meaning you can modify them in place.
In this article, I have explained how to update the value of an existing Key Dictionary. You only need to reassign a new value to that key. Python dictionaries allow for this direct modification.
Python Update Dictionary Value by Key
Here, we are explaining methods that explain how to Update Dictionary Value by Key in Python those are following.
- Using The Square Brackets
- Using U
pdate()
Method - Using a Conditional Statement
- Using S
etdefault
Method
Update Dictionary by Key Using The Square Brackets
In this example, I have created a dictionary student_info, and we updated the 'grade' key to the new value 'A' using the square bracket notation.
Python
# Creating a student information dictionary
student_info = {'name': 'Geek', 'age': 21, 'grade': 'B'}
# Updating the 'grade' key with value 'A'
student_info['grade'] = 'A'
# Displaying the updated dictionary
print(student_info)
Output{'grade': 'A', 'age': 21, 'name': 'Geek'}
Update Dictionary by Key Using Update()
Method
In this example, below code updates the 'grade' value in the student_info
dictionary to 'A' using the update()
Method and then its prints the updated dictionary.
Python
# Sample dictionary
student_info = {'name': 'Geek', 'age': 21, 'grade': 'B'}
# Using the update method to update the value for 'key2'
student_info.update({'grade': 'A'})
# Printing the updated dictionary
print(student_info)
Output{'name': 'Geek', 'age': 21, 'grade': 'A'}
Update Dictionary by Key Using a Conditional Statement
In this example, below code checks if the key 'grade' is in the `student_info` dictionary and, if true, updates its value to 'A'; then, it prints the updated dictionary `student_info`we used for these conditional statement .
Python
# Sample dictionary
student_info = {'name': 'Geek', 'age': 21, 'grade': 'B'}
# Updating the value for 'key2' using a conditional statement
if 'grade' in student_info:
student_info['grade'] = 'A'
# Printing the updated dictionary
print(student_info)
Output :
{'name': 'Geek', 'age': 21, 'grade': 'A'}
Update Dictionary by Key Using Setdefault
Method
In this example code uses the `setdefault()` method to update the 'grade' value to 'A' in the `student_info` dictionary if the key 'grade' is not already present, and then prints the updated dictionary.
Python
# Sample dictionary
student_info = {'name': 'Geek', 'age': 21, 'grade': 'B'}
# Using setdefault to update the value for 'key2'
student_info.setdefault('grade', 'A')
# Printing the updated dictionary
print(student_info)
Output :
{'name': 'Geek', 'age': 21, 'grade': 'A'}
Note: In case, since the key 'grade' already exists, setdefault() does not update its value to 'A'. Instead, it leaves the value as 'B'. So, If you want to update the value of a key regardless of whether it exists or not, you should directly assign the new value to the key as shown above example 1 [Update Dictionary by Key Using The Square Brackets].
Similar Reads
Sort Python Dictionary by Key or Value - Python There are two elements in a Python dictionary-keys and values. You can sort the dictionary by keys, values, or both. In this article, we will discuss the methods of sorting dictionaries by key or value using Python.Sorting Dictionary By Key Using sort()In this example, we will sort the dictionary by
6 min read
Python dictionary values() values() method in Python is used to obtain a view object that contains all the values in a dictionary. This view object is dynamic, meaning it updates automatically if the dictionary is modified. If we use the type() method on the return value, we get "dict_values object". It must be cast to obtain
2 min read
Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to
4 min read
Get Key from Value in Dictionary - Python The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value. Using next() with a Generator ExpressionThis is the most efficient when we only need the first matching key. This meth
5 min read
Python | Set 4 (Dictionary, Keywords in Python) In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python. Dictionary in Python In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value c
5 min read