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 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() to create a shallow copy of the dictionary, and the return value will be passed as an argument to the append() to add it to the list.

Related: Copy the Dictionary and edit it in Python

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 use the dict() to do a shallow copy of the dictionary and use this copied dictionary to the append() method to add the dictionary to the end of the list. This method also yields the same output as above.

# Append the dictionary to list using dict()
list = []
org_dict = {“course”: “python”, “fee”: 4000}
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 copy the dictionary and append it to the list using the append() method. Now we will see what happens when we append a dictionary directly without copying to a list and updating dictionary values.

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

# 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}

Note that the change we made to the dictionary in the list also reflected on the original dictionary hence, it is recommended to use the deep/shallow copy before appending the dictionary to the Python list.

5. Append Dictionary to list using + Operator

We can also 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. If we update the dictionary within a list, which will not reflect the original dictionary.

# Import copy
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

Finally, we can append the dictionary to the list manually using for loop. Below is an example

# 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. When you append a dictionary as-is without copying, it appends by reference and any changes made to the dictionary also affect the original dictionary.

 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 *