In Python, the dictionary and list data types are used to store the multiple sequence value. The dictionary is a collection of key-value pairs that are unordered, changeable, and do not allow duplicate keys. At the same time, a list is an ordered collection of items that can contain duplicate elements and is changeable. Various methods are utilized in Python to append a dictionary to a list.
This write-up will give you various approaches to append a dictionary to Python lists. The content follows in this article is listed below:
- Method 1: Using “append()” Method
- Method 2: Using “append()” and deepcopy() Method
- Method 3: Using List Comprehension
Let’s start with the first method.
Method 1: Using “append()” Method
The “append()” method is used to append a dictionary value to a list in Python. An example given below shows the implementation:
Code:
dict_value = {'Name': 'Alex', 'Age': 45, 'Height': 5.7}
list_value = ['A', 'B', 'C']
list_value.append(dict_value)
print(list_value)
In the above example, the “append()” function takes the dictionary variable “dict_value” as an argument and appends it into a list named “list_value”.
Output:
The dictionary value has been appended into a list in the above output using the “append()” function.
Method 2: Using “append()” and deepcopy() Method
The copy module function “deepcopy()” creates the copy of the dictionary variable and appends it to the list without changing the original dictionary. In the example code given below, the “append()” and “deepcopy()” function is used to append a “nested dictionary” into a list:
Code:
import copy
dict_value = {'Name': 'Alex', 'info': {'Age': 45, 'Height': 5.7}}
list_value = ['A', 'B', 'C']
list_value.append(copy.deepcopy(dict_value))
print(list_value)
In the above code, the “copy.deepcopy()” function accepts the dictionary variable and returns the copy of the dictionary that will be appended to the list using the “append()” function.
Output:
The above output verified that the nested dictionary had been appended to the list using the “deepcopy()” and “append()” functions.
Method 3: Using List Comprehension
In Python, the “zip()” function joins the pair of iterators. An iterator of tuples is returned by zip as an object. In the example given below, the “zip()” function along with the “for” loop is used to append a dictionary into a variable:
Code:
list_value = ['A', 'B', 'C']
value = [dict(zip([1],[x])) for x in list_value]
print(value)
In the above code:
- The “zip()” function takes the constant integer value as a first argument and items of the list as a second argument.
- On each iteration, the element value will be changed, but the integer “1” first parameter value remains the same.
- The “dict()” function converts the zip object into a dictionary in key-value format.
- In this way, the dictionary is appended to the list by using the list comprehension and “zip()” function.
Output:
The dictionary having constant key name “1” and changing value according to items of the list has been added to the empty list.
Note: The “append()” function is also used after the list comprehension to append the returned list variable “value” into another list using the “append()” function.
That’s how a Python dictionary is appended to a List.
Conclusion
To append a dictionary to a list in Python, the “append()”, “deepcopy()”, and List comprehension methods are used. The “append()” method is the simplest and easiest method to append the dictionary into a list by passing the dictionary as a parameter value. Creating a copy of a dictionary and appending it to a list without changing the original dictionary is done using the “deepcopy()” along with the “append()” method. This post presented a detailed guide on appending a dictionary to a list.