Thursday, December 26, 2024
Google search engine
HomeLanguagesAppending a dictionary to a list in Python

Appending a dictionary to a list in Python

In this article, we will discuss how to append the dictionary to the list data structure in Python.

Here we will discuss:

  1. Appending a dictionary to a list with the same key and different values
  2. Using append() method
  3. Using copy() method  to list using append() method
  4. Using deepcopy() method to list using append() method
  5. Using NumPy.

Method 1: Appending a dictionary to a list with the same key and different values

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 list
l = [dict(zip([1],[x])) for x in range(1,100)]
 
# display list
print(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}]

Method 2: Using append() method

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 list
l = []
 
# create a dictionary with student details
student = {7058:'sravan kumsr Gottumukkala',
           7059:'ojaswi',7060:'bobby',
           7061:'gnanesh',7062:'rohith'}
 
# append this dictionary to the empty list
l.append(student)
 
# display list
l


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 elements
l = [1, 2, "hi", "welcome"]
 
# create a dictionary with student details
student={7058:'sravan kumsr Gottumukkala',
         7059:'ojaswi',7060:'bobby',
         7061:'gnanesh',7062:'rohith'}
 
# append this dictionary to the empty list
l.append(student)
 
# display list
l


Output:

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Method 3: Using copy() and append() method

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 list
l = []
 
# create a dictionary with student details
student = {7058:'sravan kumsr Gottumukkala',
           7059:'ojaswi',7060:'bobby',
           7061:'gnanesh',7062:'rohith'}
 
# append this dictionary to the
# empty list using copy() method
l.append(student.copy())
 
# display list
l


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 elements
l = [1, 2, "hi", "welcome"]
 
# create a dictionary with student details
student = {7058:'sravan kumsr Gottumukkala',
           7059:'ojaswi',7060:'bobby',
           7061:'gnanesh',7062:'rohith'}
 
# append this dictionary to
# the empty list using copy() method
l.append(student.copy())
 
# display list
l


Output:

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Method 4: Using deepcopy() method and append() method

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 module
from copy import deepcopy
 
# create an empty list
l = []
 
# create a dictionary with student details
student = {7058: 'sravan kumsr Gottumukkala',
           7059: 'ojaswi', 7060: 'bobby',
           7061: 'gnanesh', 7062: 'rohith'}
 
# append this dictionary to
# the empty list using deepcopy() method
l.append(deepcopy(student))
 
# display list
l


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 module
from copy import deepcopy
 
# create an list that contain some elements
l = [1, 2, "hi", "welcome"]
 
# create a dictionary with student details
student = {7058:'sravan kumsr Gottumukkala',
           7059:'ojaswi',7060:'bobby',
           7061:'gnanesh',7062:'rohith'}
 
# append this dictionary to the empty
# list using deepcopy() method
l.append(deepcopy(student))
 
# display list
l


Output:

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Method 5: Using Numpy

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 library
import numpy as np
 
# define a list
subjects = ["PHP", "Java", "SQL"]
 
# iterating the elements in
# list to create an numpy array
data = np.array([{'GFG': i} for i in subjects])
 
# append to the numpy array to list
final = np.append(res_array, {'GFG': "ML/DL"}).tolist()
 
# Printing the appended data
print(final)


 

 

Output:

 

[{‘GFG’: ‘PHP’}, {‘GFG’: ‘Java’}, {‘GFG’: ‘SQL’}, {‘GFG’: ‘ML/DL’}]

 

RELATED ARTICLES

Most Popular

Recent Comments