Change all values in dictionary python

Python Dictionary update() Method

Python dictionary update() method is used to update the dictionary using another dictionary or an iterable of key-value pairs(such as a tuple). If the key is already present in the dictionary, the update() method changes the existing key with the new value.

Python dictionary is an unordered collection of elements, that is used to store elements like key-value pairs, which is not the same as other data types that contain only a single value.

In this article, I will explain the Python dictionary update() method with examples. It does not return any values, in addition, it updates the same dictionary with the newly associated key-value pairs

All dictionary methods are available on the dictionary methods page..

Quick Examples of Python Dictionary update()

Following are the quick examples of the Python dictionary update() method.

 # Example 1: Update the dictionary Technology = Technology.update() # Example 2: Update the existing key of the dictionary Technology= Technology.update() # Example 3: Update using key-value pair Technology= Technology.update([('duration','45days'),('tutor','Richard')]) 

1. Syntax of Dictionary update()

Following is the syntax of the Python dictionary update() method.

 # syntax of the dictionary update() dict.update(iterable) 

1.2 Parameters of Python Dictionary update()

Python dictionary update() allows one parameter.

Iterable :(optional) It is either dictionary or an iterable key-value pair.

1.3 Return Value of Python Dictionary update()

2. Usage of Python Dictionary update() Method

Using Python Dictionary update() method we can update the dictionary by using another dictionary or an iterable of key-value pairs(such as a tuple). If the key is already present, this method updates the existing key with the new value from iterable value.

2.1 Add New Element to the Dictionary

The below example demonstrates how to add new elements to the existing dictionary.

 # Using update() update the dictionary Technology = Technology.update() print("Updated Technology:",Technology) # Outputs: Updated Technology:

2.2 Update the Existing key of Dictionary

When key is already present in the dictionary update() method updates the existing key with key-value pair. Let’s see with an example.

 # update the existing key of the dictionary Technology= Technology.update() print("Updated Technology:",Technology) # Outputs: Updated Technology:

In this example duration key existed with the value 45 days , which has been updated with the value 65 days .

Читайте также:  Driver find element by css selector

3. Using tuple as a Param

Update() method also updates the dictionary when passing an iterable object of key-value pair(tuple) as a parameter. Let’s take an example.

 # update the dictionary by passing iterable of key-value pair Technology= Technology.update([('duration','45days'),('tutor','Richard')]) print("Updated Technology:",Technology) # Output: Updated Technology:

From the above code, we have passed tuple as a param to Python dictionary update() method, this added new elements duration and tutor to the dictionary.

4. Conclusion

In this article, I have explained the Python Dictionary update() method with examples. By using a dictionary or an iterable of key-value pairs(such as a tuple) to update the dictionary? I have also explained how to update the existing key with updated key-value pair.

References

You may also like reading:

Источник

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.

Источник

Change Dictionary Values in Python

Change Dictionary Values in Python

  1. Change Dictionary Values in Python Using the dict.update() Method
  2. Change Dictionary Values in Python Using the for Loop
  3. 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_dictChange all values in dictionary python = 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

Источник

Оцените статью