Skip to content

How to Append Dictionary to List in Python? Vijetha Spark By {Examples}

  • by

We can append/add a dictionary to the list using the list.append()method of Python. We know that Python lists are mutable and allow different types of data types as their values. Since it is mutable, we can add elements to the list or delete elements from the list.

In this article, I will explain how to append a dictionary to a list using append() and other methods with multiple examples.

1. Quick Examples of Append Dictionary to List

Following are quick examples of how to add a dictionary to a list.

# Below are the quick examples.

# Example 1: Add dictionary to empty list
list = []
dict = {“course”: “python”, “fee”: 4000}
list.append(dict.copy())

# Example 2: Append the dictionary to list using dict()
list = []
org_dict = {“course”: “python”, “fee”: 4000}
copy_dict = dict(org_dict)
list.append(copy_dict)

# Example 3: Append the dictionary to list without copying
list.append(dict)
list [0][“course”] = “pandas”

# Example 4: Append the dictionary to list
dict = {“course”: “python”, “fee”: 4000}
list += [dict]

# Example 5: Using copy.deepcopy()
list = []
dict = {“course”: “python”, “fee”: {“discount”: 2000}}
dict_copy = copy.deepcopy(dict)
list.append(dict_copy)

# Example 6: Append the dictionary to list using for loop
list = []
for i in range(10):
list.append({1: i})
print(list)

2. Append Python Dictionary to List using copy() Method

The list.append() method is used to append an item to the list, so we can use this to append/add a dictionary to the list. In this first example, I will use the dict.copy() method that is used to create a shallow copy of the dictionary and the return value will be added to the list.

The below example appends the copy of the dictionary to list by value, not by reference. Hence, any update to the dictionary values on the list will not have an impact on the original list.

# Add dictionary to the empty list
list = []

dict = {“course”: “python”, “fee”: 4000}
list.append(dict.copy())
print(“List:”, list)

Yields below output.

3. Append Python Dictionary to List Using dict() constructor

We can also append the dictionary to the list using the dict() constructor, it will create a shallow copy of the dictionary. Then using append() method we can append the copy of the dictionary to the list. It gives same result as the above.

# Empty list
list = []
# Create dictionary
org_dict = {“course”: “python”, “fee”: 4000}
# Append the dictionary to list using dict()
copy_dict = dict(org_dict)
list.append(copy_dict)
print(“List:”, list)

Yields below output.

4. Append Python Dictionary to List without copying

So far, we have learned how to append a copy of the dictionary to the list using the append() method. Now we will see what happens when we append a dictionary directly without copying to a list using append() method.

Let’s take the append() method and pass the given dictionary as an argument it, will append the reference of the dictionary to the list. which means that any modification occurs in the dictionary of the appended list which will be reflected in the original dictionary also.

# Append the dictionary to list without copying
list.append(dict)
print(“List:”, list)
# Update the appended list
list [0][“course”] = “pandas”
print(“List:”, list)
print(“Dictionary:”, dict)

# Output:
# List: [{‘course’: ‘python’, ‘fee’: 4000}]
# List: [{‘course’: ‘pandas’, ‘fee’: 4000}]
# Dictionary: {‘course’: ‘pandas’, ‘fee’: 4000}

5. Append Dictionary to list using + Operator

We can append the dictionary to list using ‘+’ operator. Let’s take an example,

# Append the dictionary to list
dict = {“course”: “python”, “fee”: 4000}
list += [dict]
print(“List:”, list)

# Output:
# List: [{‘course’: ‘python’, ‘fee’: 4000}]

6. Using deepcopy() Method and append() Method

We can append the nested dictionary to the list using the deepcopy() method. When our dictionary contains other objects like lists, class instances or other dictionaries and we want to append it to a list using copy.deepcopy() method. It will create a deep copy of the provided object. If we update the dictionary within a list, which will not reflect the original dictionary.

import copy
# Empty list
list = []
# Create nested dictionary
dict = {“course”: “python”, “fee”: {“discount”: 2000}}
dict_copy = copy.deepcopy(dict)
# Append the nested dictionary using deepcopy()
list.append(dict_copy)
print(list)

# Update the Appended list
list[0][‘fee’][‘discount’] = 1500
print(“Updated element of list:”, list[0][‘fee’][‘discount’])
print(“Old element of Dictionary:” ,dict[‘fee’][‘discount’])
print(“Updated list:”, list)

# Output:
# List: [{‘course’: ‘python’, ‘fee’: {‘discount’: 2000}}]
# Updated element of list: 1500
# Old element of Dictionary: 2000
# Updated list: [{‘course’: ‘python’, ‘fee’: {‘discount’: 1500}}]

From the above, the dictionary has been appended to the list by value, and not by reference.

7. Append Dictionary to Python List using for loop

Alternatively, we can append the dictionary to the list using for loop. To iterate a specified range of numbers using for loop, for each iteration numbers will append the list in the form of key/value pair.

# Append the dictionary to list using for loop
# Empty list
list = []
for i in range(10):
list.append({1: i})
print(“List:”, list)

# Output:
# List: [{1: 0}, {1: 1}, {1: 2}, {1: 3}, {1: 4}, {1: 5}, {1: 6}, {1: 7}, {1: 8}, {1: 9}]

8. Conclusion

In this article, I have explained how to append a dictionary to a list using the append() method with multiple examples. And also explained using copy() method how we can append the dictionary to list by value and not a reference.

 We can append/add a dictionary to the list using the list.append()method of Python. We know that Python lists are mutable and allow different types of data types as their values. Since it is mutable, we can add elements to the list or delete elements from the list. In this article, I will explain how to  Read More Python, Python Tutorial, Python Dictionary Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *