Open In App

Append a Value to a Dictionary Python

Last Updated : 10 May, 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.



Similar Reads

Python - Append Dictionary Keys and Values ( In order ) in dictionary
Given a dictionary, perform append of keys followed by values in list. Input : test_dict = {"Gfg" : 1, "is" : 2, "Best" : 3} Output : ['Gfg', 'is', 'Best', 1, 2, 3] Explanation : All the keys before all the values in list. Input : test_dict = {"Gfg" : 1, "Best" : 3} Output : ['Gfg', 'Best', 1, 3] Explanation : All the keys before all the values in
5 min read
Python - Append Multitype Values in Dictionary
Sometimes, while working with Python dictionaries, we can have problem in which we need to update the dictionary values, according to the data type. This type of problem can have application in many domains which include data. Lets discuss a way in which this problem can be solved. Input : test_dict = {'gfg' : "", 'is' : {}, 'best' : []} Output : {
4 min read
Python - Append items at beginning of dictionary
Given a dictionary, append another dictionary at beginning of it. Input : test_dict = {"Gfg" : 5, "is" : 3, "best" : 10}, updict = {"pre1" : 4} Output : {'pre1': 4, 'Gfg': 5, 'is': 3, 'best': 10} Explanation : New dictionary updated at front of dictionary. Input : test_dict = {"Gfg" : 5}, updict = {"pre1" : 4} Output : {'pre1': 4, 'Gfg': 5} Explana
3 min read
Append list of dictionary and series to a existing Pandas DataFrame in Python
In this article, we will discuss how values from a list of dictionaries or Pandas Series can be appended to an already existing pandas dataframe. For this purpose append() function of pandas, the module is sufficient. Syntax: DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None) Parameters :other : DataFrame or Series/dict-
2 min read
Append a Key to a Python Dictionary
We can append or add and update the value of key in dictionary using various methods. In this article, we will explore three different methods to achieve this task: Using update() method, Using merge operator ("|"), Using update operator("|="), Adding key with value, Adding key without value. Example : Input: Dictionary ={ 'gfg': 3, 'geeks': 5, 'fo
3 min read
Convert Dictionary Value list to Dictionary List Python
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in which this task can be performed. Input : test_list =
9 min read
Python - Replace dictionary value from other dictionary
Given two dictionaries, update the values from other dictionary if key is present in other dictionary. Input : test_dict = {"Gfg" : 5, "is" : 8, "Best" : 10, "for" : 8, "Geeks" : 9}, updict = {"Geeks" : 10, "Best" : 17} Output : {'Gfg': 5, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 10} Explanation : "Geeks" and "Best" values updated to 10 and 17. Inpu
6 min read
Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
Given two dictionaries. The task is to merge them in such a way that the resulting dictionary contains the key from the first dictionary and the value from the second dictionary. Examples: Input : test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100}, test_dict2 = {"Gfg2" : 26, "is2" : 20, "best2" : 70} Output : {'Gfg': 26, 'is': 20, 'best': 70} Expla
8 min read
Python | Pretty Print a dictionary with dictionary value
This article provides a quick way to pretty How to Print Dictionary in Python that has a dictionary as values. This is required many times nowadays with the advent of NoSQL databases. Let's code a way to perform this particular task in Python. Example Input:{'gfg': {'remark': 'good', 'rate': 5}, 'cs': {'rate': 3}} Output: gfg: remark: good rate: 5
7 min read
Python - Extract ith Key's Value of K's Maximum value dictionary
Given Dictionary List, extract i'th keys value depending upon Kth key's maximum value. Input : test_list = [{"Gfg" : 3, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}, {"Gfg" : 9, "is" : 16, "best" : 1}], K = "best", i = "is" Output : 11 Explanation : best is max at 19, its corresponding "is" value is 11. Input : test_list = [{"Gfg" :
9 min read
Practice Tags :