Open In App

Append a Value to a Dictionary Python

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Dictionaries in Python are versatile data structures that allow you to store and retrieve key-value pairs efficiently. While dictionaries are mutable, meaning they can be modified after creation, adding new values to a dictionary requires some specific methods. In this article, we’ll explore four commonly used methods to append a value to a dictionary in Python.

How to Append a Value to a Dictionary Python?

Below, are the methods of How to Append a Value to a Dictionary Python.

Append a Value to a Dictionary Python Using Square Bracket Notation

The most straightforward way to add a key-value pair to a dictionary is by using the square bracket notation. If the key does not exist in the dictionary, it will be added along with the associated value. If the key already exists, the existing value will be overwritten with the new one

Python3




# Example dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
 
# Append a new key-value pair
my_dict['d'] = 4
 
print(my_dict)


Output

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Append a Value to a Dictionary Python Using update() Method

The update() method allows you to add multiple key-value pairs to a dictionary at once. It takes another dictionary or an iterable of key-value pairs as an argument.

Python3




# Example dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
 
# Append new key-value pairs using update()
my_dict.update({'d': 4, 'e': 5})
 
print(my_dict)


Output

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

Append a Value to a Dictionary Python Using setdefault() Method

The setdefault() method is particularly useful when you want to append a value to a dictionary only if the key does not already exist. If the key is present, it returns the existing value without modifying the dictionary.

Python3




# Example dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
 
# Append a new key-value pair using setdefault()
my_dict.setdefault('d', 4)
 
print(my_dict)


Output

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Append a Value to a Dictionary Python Using dict() Constructor

You can also use the dict() constructor to append key-value pairs to a dictionary. This method is useful when you want to create a new dictionary with additional key-value pairs.

Python3




# Example dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
 
# Append a new key-value pair using dict() constructor
my_dict = dict(my_dict, d=4)
 
print(my_dict)


Output

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Conclusion

Appending values to a dictionary in Python can be accomplished using several methods, each with its own use case. Whether you prefer the simplicity of square bracket notation, the flexibility of the update() method, the conditional behavior of setdefault(), or the constructor approach, these methods provide you with the tools to modify dictionaries dynamically in your Python programs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads