/  Technology   /  Append a Dictionary to a list in Python
Append a Dictionary to a list in Python 5 (i2tutorials)

Append a Dictionary to a list in Python

Appending a dictionary to a list in python can perform this activity in various ways. In this article, we will the below functionalities to achieve this.

 

  • List comprehensions
  • Append function
  • ZIP method
  • Numpy Library

 

1. List comprehensions

List comprehensions are one line statements that contains the loop and conditional statements finally sorting the results into a list. This is commonly used as a replacement of plain for loop to minimize the memory and processing usage.

 

Program:

# Create a list variable that holds list of values
My_list = ["Hello there!", "Welcome to India", "How can i help you"]

# List Comprehension to iterate through list variable and creating the key values to form a list of dictionary values
responses = [{'text': i} for i in My_list]

#append method for appending the dictionaries into list
responses.append({'text': "Here you go"})

#Printing the list of dictionaries 
print(responses)

 

Output:

 

2. Append Function

Append Function is used to add new values to the existing list variable that already hold empty, one or more values.


Program:

# create a list variable that holds list of values
My_list = ["Hello there!", "Welcome to India", "How can i help you"]

#Created a list variable that holds a Dictionary as a value
responses=[{'text': 'Here you go'}]

#For loop to iterate through list variable and assigning the key values to form a dictionary 
for x in My_list:
       My_dict['text'] = x
       Fin_list.append(My_dict) 
       #append method for appending the dictionaries into list
       My_dict={}
    
#Printing the list of dictionaries 
print(responses)

 

Output:

 

 

3. Using Zip() method

Another approach of generating list of dictionaries using Zip() method.

Zip() is a method in Python that is used to map the similar index of multiple containers. Here, let’s consider those containers as lists.

 

Program:

# create a list variable that holds list of values
My_list = ["Hello there!", "Welcome to India", "How can i help you"]

#Below is the list comprehension to iterate through the list of values in My_list and zip those values togather with 'text'
#Finally we are converting them into Dictionary
responses=[dict(zip(['text'],[x])) for x in My_list]

#Printing the list of dictionaries 
print(responses) 

 

Output:

 

 

4. Numpy Library

Numpy (Numerical Python) is famous for its exclusive array implementations in python programming. We are much aware that main core programming language of python does not support arrays rather we consider the lists as the replacement of arrays. Here, Numpy can provide us the Solutions with the help of arrays and its large set of supporting functions.

 

Program:

#import numpy library 
import numpy as np
My_list = ["Hello there!", "Welcome to India", "How can i help you"]
res_array = np.array([{'text': i} for i in My_list])
responses=np.append(res_array, {'text': "Here you go"}).tolist()
#Printing the list of dictionaries 
print(responses)

 


Output:

Feel free to send your request to coding experts who will help with your Python Assignment.

Leave a comment