Open In App

Appending into a List in Python Dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

Python dictionaries are powerful data structures that allow you to store and retrieve data using key-value pairs. Appending an item to a list within a dictionary involves accessing the list by its corresponding key and then using the append() method to add the new item to the list in Python.

Example

Input: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20]}
Output: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20, 'Twenty']}
Explaination: Appended "Twenty" in the list of keys of Age.

How to Append into a List in Dictionary

In Python, we can append to a list in a dictionary in several ways, we are explaining some generally used methods which are used for appending to a list in Python Dictionary.

  1. Using += Operator
  2. Using List append() Method
  3. Using defaultdict() Method
  4. Using update() Function
  5. Using dict() Method
  6. Using extend() Method
  7. Using List Comprehension

Append to a List in Dictionary using += Operator

In this method, we will use the “+=” operator to append a Python list into the dictionary, for this we will take a dictionary and then add elements as a list into the dictionary.

Example: In this example, the below code initializes a dictionary ‘Details’ with key-value pairs. It then appends the list [20, “Twenty”] to the ‘Age’ key in the dictionary and prints the original and modified versions of ‘Details’.

Python3




Details = {"Destination": "China",
           "Nationality": "Italian", "Age": []}
 
print("Original:", Details)
 
# appending the list
Details["Age"] += [20, "Twenty"]
print("Modified:", Details)


Output:

Original: {'Destination': 'China', 'Nationality': 'Italian', 'Age': []}
Modified: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20, 'Twenty']}

Appending a Dictionary to a List in Python using the List append() Method

In this method, we will use conditions for checking the key and then append to a dictionary list using the list append() method.

Example: In this example, the below code creates a dictionary named “Details” and, if “Age” is a key, appends “Twenty” to the corresponding list, printing the modified dictionary.

Python3




Details = {"Destination": "China",
           "Nationality": "Italian", "Age": [20]}
print("Original:", Details)
 
if "Age" in Details:
    Details["Age"].append("Twenty")
    print("Modified:", Details)


Output:

Original: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20]}
Modified: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [20, 'Twenty']}

Append a dictionary to a list in Python using defaultdict() Method

In this method, we are using the defaultdict() function. It is a part of the collections module. We have to import the function from the collections module to use it in the program and then use it to append to a dictionary list. Since append takes only one parameter, to insert another parameter, repeat the append method.

Example: In this example, the below code uses a defaultdict() to create a dictionary called “Details” with default values as empty lists. It then appends “India” and “Pakistan” to the “Country” key’s list, showcasing the modified dictionary.

Python3




from collections import defaultdict
 
Details = defaultdict(list)
print("Original:", Details)
 
Details["Country"].append("India")
Details["Country"].append("Pakistan")
print("Modified:", Details)


Output:

Original: defaultdict(<class 'list'>, {})
Modified: defaultdict(<class 'list'>, {'Country': ['India', 'Pakistan']})

Python Append to a List in Dictionary using update() Function

We will use the Python dictionary update() function to add a new list to the dictionary. We can use the update() function to embed a dictionary inside another dictionary. 

Example: In this example below code initializes a dictionary with destination and nationality, adds an empty list to the “Age” key, prints the original dictionary, and updates “Age” with a list of ages using `update()`, resulting in a modified dictionary.

Python3




Details = {"Destination": "China",
           "Nationality": "Italian"}
Details["Age"] = []
print("Original:", Details)
 
# using update() function
Details.update({"Age": [18, 20, 25, 29, 30]})
print("Modified:", Details)


Output:

Original: {'Destination': 'China', 'Nationality': 'Italian', 'Age': []}
Modified: {'Destination': 'China', 'Nationality': 'Italian', 'Age': [18, 20, 25, 29, 30]}

Append to a Dictionary in Python using dict() function

You can convert a list into a value for a key in a Python dictionary using dict() function. 

Example: In this example below code creates a dictionary named “Details” with the key “Age” and values from the list [18, 20, 25, 29, 30], then prints the resulting dictionary.

Python3




Values = [18, 20, 25, 29, 30]
Details = dict({"Age": Values})
print(Details)


Output:

{'Age': [18, 20, 25, 29, 30]}

Appending to a List in a Dictionary using extend() Method

The extend() method is used to append elements from an iterable, such as a list, to the end of another list. It modifies the original iterable in place by adding elements from the provided iterable.

Example : In this example below code initializes a dictionary “Details” with a key “Age” and values as a list. It then extends the list with additional values [35, 40] and prints the modified dictionary.

Python3




Details = {"Age": [18, 20, 25, 29, 30]}
Details["Age"].extend([35, 40])
print(Details)


Output :

{'Age': [18, 20, 25, 29, 30, 35, 40]}

Append Values to List in Dictionary using List Comprehension

Appending values to a list within a Python dictionary, using list comprehension involves creating a new list by iterating over existing elements and adding the desired values.

Example: In this example, the list comprehension [x for x in Details[“Age”]] iterates over each element in the existing “Age” list. Then, the list is extended by adding the values from the extra_values list.

Python3




Details = {"Age": [18, 20, 25, 29, 30]}
extra_values = [35, 40]
 
# Using List Comprehension to append values to the existing list
Details["Age"] = [x for x in Details["Age"]] + extra_values
 
print(Details)


Output :

{'Age': [18, 20, 25, 29, 30, 35, 40]}

We have covered various ways of appending a list in the dictionary in Python. You can easily add values to a list in the dictionary. This makes it much easier to integrate iterables into each other and update them individually according to needs.

Read More:



Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads