Table of Contents
Converting a list to a NumPy array in Python involves using the “array()” function from the NumPy library. This function takes in a list as an argument and returns a NumPy array. This conversion allows for efficient manipulation and analysis of data as NumPy arrays have specialized methods and functions.
Here is an example of how a list can be converted to a NumPy array:
list1 = [1, 2, 3, 4, 5]
array1 = np.array(list1)
This will create a NumPy array, array1, containing the elements of list1. NumPy arrays can also be multidimensional, so a list of lists can be converted to a multidimensional NumPy array.
list2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
array2 = np.array(list2)
This will create a 3×3 multidimensional NumPy array, array2, containing the elements of list2.
In conclusion, converting a list to a NumPy array is a simple process that can be done using the “array()” function from the NumPy library. This allows for efficient manipulation and analysis of data, making it a useful tool for data analysis and scientific computing.
Convert List to NumPy Array (With Examples)
You can use the following basic syntax to convert a list in Python to a NumPy array:
import numpy as np my_list = [1, 2, 3, 4, 5] my_array = np.asarray(my_list)
The following examples shows how to use this syntax in practice.
Example 1: Convert List to NumPy Array
The following code shows how to convert a list in Python to a NumPy array:
import numpy as np #create list of values my_list = [3, 4, 4, 5, 7, 8, 12, 14, 14, 16, 19] #convert list to NumPy array my_array = np.asarray(my_list) #view NumPy arrayprint(my_array) [ 3 4 4 5 7 8 12 14 14 16 19] #view object typetype(my_array) numpy.ndarray
Note that you can also use the dtype argument to specify a certain data type for the new NumPy array when performing the conversion:
import numpy as np #create list of values my_list = [3, 4, 4, 5, 7, 8, 12, 14, 14, 16, 19] #convert list to NumPy array my_array = np.asarray(my_list, dtype=np.float64) #view data type of NumPy array print(my_array.dtype) float64
Example 2: Convert List of Lists to NumPy Array of Arrays
The following code shows how to convert a list of lists to a NumPy array of arrays:
import numpy as np #create list of lists my_list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] #convert list to NumPy array my_array = np.asarray(my_list_of_lists) #view NumPy array print(my_array) [[1 2 3] [4 5 6] [7 8 9]]
We can then use the shape function to quickly get the dimensions of the new array of arrays:
print(my_array.shape)
(3, 3)
This tells us that the NumPy array of arrays has three rows and three columns.
The following tutorials explain how to perform other common data conversions in Python:
Cite this article
stats writer (2024). How do you convert a list to a NumPy array in Python? Can you provide some examples of how this can be done?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-convert-a-list-to-a-numpy-array-in-python-can-you-provide-some-examples-of-how-this-can-be-done/
stats writer. "How do you convert a list to a NumPy array in Python? Can you provide some examples of how this can be done?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-do-you-convert-a-list-to-a-numpy-array-in-python-can-you-provide-some-examples-of-how-this-can-be-done/.
stats writer. "How do you convert a list to a NumPy array in Python? Can you provide some examples of how this can be done?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-convert-a-list-to-a-numpy-array-in-python-can-you-provide-some-examples-of-how-this-can-be-done/.
stats writer (2024) 'How do you convert a list to a NumPy array in Python? Can you provide some examples of how this can be done?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-convert-a-list-to-a-numpy-array-in-python-can-you-provide-some-examples-of-how-this-can-be-done/.
[1] stats writer, "How do you convert a list to a NumPy array in Python? Can you provide some examples of how this can be done?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How do you convert a list to a NumPy array in Python? Can you provide some examples of how this can be done?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
