Changing dictionary values in Python
This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods.
-
dict.update()
method -
for
cycle. - Dictionary Unpacking
dict.update()
How to change dictionary values
in Python using
In this method, we pass the new key-value pair to update()
the method of the dictionary object. We can dict.update()
change one or more key-value pairs using the method.
Sample code:
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)
Output:
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}
for
Changing dictionary values
using loop in Python
In this method, we use for
a loop to continuously iterate through the dictionary until we find the key whose value we want to modify. After getting the key, we can change its value by assigning a new value to the key.
Code example:
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_dict[key] = 10
print(my_dict)
Output:
{'Khan': 4, 'Ali': 10, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1}
Changing dictionary values in Python by using *
unpacking operators
In this method, we can *
change the value of the dictionary by unpacking the dictionary using the operator and then adding one or more key-value pairs that we want to change the dictionary.
Sample code:
my_dict = {"Khan": 4, "Ali": 2, "Luna": 6, "Mark": 11, "Pooja": 8, "Sara": 1}
my_dict = {**my_dict, "Pooja": 12}
print(my_dict)
Output:
{'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 12, 'Sara': 1}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding the maximum value in a Python dictionary
Publish Date:2025/05/05 Views:60 Category:Python
-
This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t
How to read input from stdin in Python
Publish Date:2025/05/05 Views:124 Category:Python
-
This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P
Maximum integer in Python
Publish Date:2025/05/05 Views:55 Category:Python
-
This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t
Get a list of time zones using Python
Publish Date:2025/05/05 Views:107 Category:Python
-
When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around
Convert NumPy array to list in Python
Publish Date:2025/05/05 Views:101 Category:Python
-
Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si
Calculating Mahalanobis distance in Python
Publish Date:2025/05/05 Views:185 Category:Python
-
This tutorial will show you how to find the Mahalanobis distance between two NumPy arrays in Python. Use the function scipy.spatial.distance in the Python library cdist() to calculate the Mahalanobis distance The Mahalanobis distance is a m
Implementing the ReLU function in Python
Publish Date:2025/05/05 Views:177 Category:Python
-
This tutorial will discuss the Relu function and how to implement it in Python. ReLU function Relu Functions are the foundation of machine learning and are essential when using deep learning. ReLU The term is Rectified Linear Unit an acrony