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