Appending a dictionary to a list in Python - GeeksforGeeks

03 Feb, 2022
In this article, we will discuss how to append the dictionary to the list data structure in Python.
Here we will discuss:
Appending a dictionary to a list with the same key and different valuesUsing append() methodUsing copy() method  to list using append() methodUsing deepcopy() method to list using append() methodUsing NumPy.
Appending a dictionary to a list with the same key and different values
Using append() method
Using copy() method  to list using append() method
Using deepcopy() method to list using append() method
Using NumPy.
Here we are going to append a dictionary of integer type to an empty list using for loop with same key but different values. We will use the using zip() function
Syntax: list=[dict(zip([key],[x])) for x in range(start,stop)]
where, key is the key and range() is the range of values to be appended
Example: Python code to append 100 values from 1 to 100 with 1 as key to list
In this example, we are taking elements from 1 to 100 as values and assigning those values to the key 1 and finally, we are zipping the elements and finally, we are appending the elements in a list.
Python3
# append 100 values from 1 to 100# with 1 as key to listl = [dict(zip([1],[x])) for x in range(1,100)] # display listprint(l)
Output:
[{1: 1}, {1: 2}, {1: 3}, {1: 4}, {1: 5}, {1: 6}, {1: 7}, {1: 8}, {1: 9}, {1: 10}, {1: 11}, {1: 12}, {1: 13}, {1: 14}, {1: 15}, {1: 16}, {1: 17}, {1: 18}, {1: 19}, {1: 20}, {1: 21}, {1: 22}, {1: 23}, {1: 24}, {1: 25}, {1: 26}, {1: 27}, {1: 28}, {1: 29}, {1: 30}, {1: 31}, {1: 32}, {1: 33}, {1: 34}, {1: 35}, {1: 36}, {1: 37}, {1: 38}, {1: 39}, {1: 40}, {1: 41}, {1: 42}, {1: 43}, {1: 44}, {1: 45}, {1: 46}, {1: 47}, {1: 48}, {1: 49}, {1: 50}, {1: 51}, {1: 52}, {1: 53}, {1: 54}, {1: 55}, {1: 56}, {1: 57}, {1: 58}, {1: 59}, {1: 60}, {1: 61}, {1: 62}, {1: 63}, {1: 64}, {1: 65}, {1: 66}, {1: 67}, {1: 68}, {1: 69}, {1: 70}, {1: 71}, {1: 72}, {1: 73}, {1: 74}, {1: 75}, {1: 76}, {1: 77}, {1: 78}, {1: 79}, {1: 80}, {1: 81}, {1: 82}, {1: 83}, {1: 84}, {1: 85}, {1: 86}, {1: 87}, {1: 88}, {1: 89}, {1: 90}, {1: 91}, {1: 92}, {1: 93}, {1: 94}, {1: 95}, {1: 96}, {1: 97}, {1: 98}, {1: 99}]
Here, we are going to append a dictionary to a list using append() method, which is used to append an item in a list.
Syntax: list.append(dictionary)
where,
list is the input list
dictionary is an input dictionary to be appended
Example 1: Python code to append a dictionary to an empty list
Here we are considering the student id as key and name as value as a dictionary, So we are appending the student dictionary the empty list using the append method
Python3
# create an empty listl = [] # create a dictionary with student detailsstudent = {7058:'sravan kumsr Gottumukkala',           7059:'ojaswi',7060:'bobby',           7061:'gnanesh',7062:'rohith'} # append this dictionary to the empty listl.append(student) # display listl
Output:
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
Example 2: Append a dictionary to the list containing elements
Here we are considering the student id as key and name as value as a dictionary. So we are appending the student dictionary the list that already contains some elements using the append method
Python3
# create an list that contain some elementsl = [1, 2, "hi", "welcome"] # create a dictionary with student detailsstudent={7058:'sravan kumsr Gottumukkala',         7059:'ojaswi',7060:'bobby',         7061:'gnanesh',7062:'rohith'} # append this dictionary to the empty listl.append(student) # display listl
Output:
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
Here, we are going to append a dictionary to a list using append() method, we can use copy() to append to a list
Syntax: dictionary.copy()
Example 1: Python code to append the dictionary to an empty list by using copy() method
Here we are considering the student id as key and name as value as a dictionary, so we are appending the student dictionary with copy method the empty list using the append method
Python3
# create an empty listl = [] # create a dictionary with student detailsstudent = {7058:'sravan kumsr Gottumukkala',           7059:'ojaswi',7060:'bobby',           7061:'gnanesh',7062:'rohith'} # append this dictionary to the# empty list using copy() methodl.append(student.copy()) # display listl
Output:
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
Example 2: Append dictionary to list those containing elements using copy() method
Here we are considering the student id as key and name as value as a dictionary, so we are appending the student dictionary with copy method the list that already contains elements using the append method
Python3
# create an list that contain some elementsl = [1, 2, "hi", "welcome"] # create a dictionary with student detailsstudent = {7058:'sravan kumsr Gottumukkala',           7059:'ojaswi',7060:'bobby',           7061:'gnanesh',7062:'rohith'} # append this dictionary to# the empty list using copy() methodl.append(student.copy()) # display listl
Output:
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
Deep copy is a process in which the copying process occurs recursively, here we are going to append the dictionary to a list by deep copying the dictionary.
Syntax: append(deepcopy())
Example 1: Append the dictionary to list using deep copy
Here we are considering the student id as key and name as value as a dictionary, so we are appending the student dictionary with deepcopy method the empty list  using the append method
Python3
# import deepcopy modulefrom copy import deepcopy # create an empty listl = [] # create a dictionary with student detailsstudent = {7058: 'sravan kumsr Gottumukkala',           7059: 'ojaswi', 7060: 'bobby',           7061: 'gnanesh', 7062: 'rohith'} # append this dictionary to# the empty list using deepcopy() methodl.append(deepcopy(student)) # display listl
Output:
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
Example 2: Appending the dictionary to the list that contains elements
Here we are considering the student id as key and name as value as a dictionary, so we are appending the student dictionary with deepcopy method the list that already contains elements using the append method
Python
# import deepcopy modulefrom copy import deepcopy # create an list that contain some elementsl = [1, 2, "hi", "welcome"] # create a dictionary with student detailsstudent = {7058:'sravan kumsr Gottumukkala',           7059:'ojaswi',7060:'bobby',           7061:'gnanesh',7062:'rohith'} # append this dictionary to the empty# list using deepcopy() methodl.append(deepcopy(student)) # display listl
Output:
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
Numpy stands for numeric python used to store and process the arrays, here we are going to use NumPy to append the dictionary.
Syntax: np.append(res_array, {‘key’: “value”}).tolist()
where,
res_array is the resultabt array
append is used to append to array
tolist() is used to convert a list
Example: Python code to append a dictionary to list using NumPy method.
Here we created a list of elements with subjects and pass the GFG key and appending to the list
Python3
# import numpy libraryimport numpy as np # define a listsubjects = ["PHP", "Java", "SQL"] # iterating the elements in# list to create an numpy arraydata = np.array([{'GFG': i} for i in subjects]) # append to the numpy array to listfinal = np.append(res_array, {'GFG': "ML/DL"}).tolist() # Printing the appended dataprint(final)
 
 
Output:
 
[{‘GFG’: ‘PHP’}, {‘GFG’: ‘Java’}, {‘GFG’: ‘SQL’}, {‘GFG’: ‘ML/DL’}]
 
sumitgumber28
Picked
python-dict
python-list
Python
Python Programs
python-dict
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?