Python Dictionary update() method
Last Updated :
09 Dec, 2024
Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Python
# update() method in Dictionary
# Dictionary with three items
d1 = {'A': 'Geeks', 'B': 'For', }
d2 = {'B': 'Geeks', 'C': 'Python'}
# update the value of key 'B'
d1.update(d2)
# using keyword arguments
d1.update(A='Hello')
print(d1)
Output{'A': 'Hello', 'B': 'Geeks', 'C': 'Python'}
Syntax of Dictionary update Method
The dictionary update() method in Python has the following syntax:
Syntax: dict.update([other])
Parameters: This method takes either a dictionary or an iterable object of key/value pairs (generally tuples) as parameters.
Returns: It doesn't return any value but updates the Dictionary with elements from a dictionary object or an iterable object of key/value pairs.
Python Dictionary update() Example
Let us see a few examples of the update() method to update the data of the Python dictionary.
Update with another Dictionary
Here we are updating a dictionary in Python using the update() method and passing another dictionary to it as parameters. The second dictionary is used for the updated value.
Python
# update() method in Dictionary
# Dictionary with three items
d1 = {'A': 'Geeks', 'B': 'For', }
d2 = {'B': 'Geeks', 'C': 'Python'}
# update the value of key 'B'
d1.update(d2)
print(d1)
Output{'A': 'Geeks', 'B': 'Geeks', 'C': 'Python'}
Update with Keyword Arguments
In this example, instead of using another dictionary, we passed an iterable value to the update() function.
Python
# Dictionary with single item
d1 = {'A': 'Geeks'}
# update the Dictionary with iterable
d1.update(B='For', C='Geeks')
print(d1)
Output{'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}
Python Dictionary Update Value if the Key Exists
In this example, we will update the value of a dictionary in Python if the particular key exists. If the key is not present in the dictionary, we will simply print that the key does not exist.
Python
# Define dictionary
d = {'m': 700, 'n': 100, 't': 500}
# Key to check
key = 'm'
# Check if key exists and update
if key in d:
print(f"Key exists. Value updated to 600.")
d[key] = 600 # Directly update the value
else:
print("Key does not exist.")
print(d)
OutputKey exists. Value updated to 600.
{'m': 600, 'n': 100, 't': 500}
Python Dictionary Update Value if the Key doesn’t Exist
Here, we will try to update the value of the dictionary whose key does not exist in the dictionary. In this case, the key and value will be added as the new element in the dictionary.
Python
# Define dictionary
d = {'m': 700, 'n': 100, 't': 500}
# Key to check
key = 'k'
# Check if key exists and update
if key not in d:
print("Key doesn't exist. Adding a new key-value pair.")
d[key] = 600 # Direct assignment
else:
print("Key exists.")
# Print updated dictionary
print(d)
OutputKey doesn't exist. Adding a new key-value pair.
{'m': 700, 'n': 100, 't': 500, 'k': 600}
Similar Reads
Python Dictionary Methods Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu
5 min read
Python Dictionary clear() clear() method in Python is used to remove all items (key-value pairs) from a dictionary. After calling this method, the dictionary will become empty and its length will be 0. This method is part of the built-in dictionary operations in Python.Example:Pythond = {1: "geeks", 2: "for"} # using clear()
2 min read
Python Dictionary copy() Python Dictionary copy() method returns a shallow copy of the dictionary. let's see the Python Dictionary copy() method with examples: Â Examples Input: original = {1:'geeks', 2:'for'} new = original.copy() // Operation Output: original: {1: 'one', 2: 'two'} new: {1: 'one', 2: 'two'}Syntax of copy()
3 min read
Python Dictionary fromkeys() Method Python dictionary fromkeys() function returns the dictionary with key mapped and specific value. It creates a new dictionary from the given sequence with the specific value. Python Dictionary fromkeys() Method Syntax: Syntax : fromkeys(seq, val) Parameters : seq : The sequence to be transformed into
3 min read
Python Dictionary get() Method Python Dictionary get() Method returns the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).Python Dictionary get() Method Syntax:Syntax : Dict.get(key, Value)Parameters: key: The key name of the item you want to return
3 min read
Python Dictionary items() method items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p
2 min read
Python Dictionary keys() method keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example:Pythond = {'A': 'Geeks', 'B': 'For', 'C': 'Ge
2 min read
Python Dictionary pop() Method The Python pop() method removes and returns the value of a specified key from a dictionary. If the key isn't found, you can provide a default value to return instead of raising an error. Example:Pythond = {'a': 1, 'b': 2, 'c': 3} v = d.pop('b') print(v) v = d.pop('d', 'Not Found') print(v) Output2 N
2 min read
Python Dictionary popitem() method popitem() method in Python is used to remove and return the last key-value pair from the dictionary. It is often used when we want to remove a random element, particularly when the dictionary is not ordered. Example:Pythond = {1: '001', 2: '010', 3: '011'} # Using popitem() to remove and return the
2 min read
Python Dictionary setdefault() Method Python Dictionary setdefault() returns the value of a key (if the key is in dictionary). Else, it inserts a key with the default value to the dictionary. Python Dictionary setdefault() Method Syntax: Syntax: dict.setdefault(key, default_value)Parameters: It takes two parameters: key - Key to be sear
2 min read