How to use Numpy In Python

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’}]

 



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.

Similar Reads

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...

Method 2: Using append() method

...

Method 3: Using copy() and 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....

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

...

Method 5: Using Numpy

...