Convert List to NumPy Array in Python (3 Examples)
Hi! This tutorial will show you 3 simple ways to turn a list into a NumPy array in the Python programming language.
First, though, here is a quick overview of this tutorial:
Let’s get right into the Python code!
Install & Import NumPy
To install and import Python’s NumPy library, run the lines of code below in your preferred Python IDE.
# install numpy pip install numpy # import numpy import numpy as np
Create Sample List
We will now create the sample list that will be turned into a NumPy array. Run the line of code below to do so.
my_list = [1, 2, 3, 4, 5]
Next, we can call the type() function that returns the class of Python objects to confirm the success of our previous attempt.
print(type(my_list)) # <class 'list'>
Example 1: Turn List into NumPy Array with array() Function
In this first example, we will turn the list into a NumPy array using the array()
function.
array = np.array(my_list) print(array) # [1 2 3 4 5] print(type(array)) # <class 'numpy.ndarray'>
The list has indeed been transformed into a NumPy array can be confirmed by running.
Example 2: Turn List into NumPy Array with asarray() Function
In this second example, we will use NumPy’s asarray()
function to turn the list into a NumPy array.
array = np.asarray(my_list) print(array) # [1 2 3 4 5] print(type(array)) # <class 'numpy.ndarray'>
The asarray()
function works very similarly to the array()
function in the first example as seen.
Example 3: Turn List into NumPy Array with fromiter() Function
In this third and final example, we will transform the list into a NumPy array using the fromiter()
function.
array = np.fromiter(my_list, dtype=int) print(array) # [1 2 3 4 5] print(type(array)) # <class 'numpy.ndarray'>
The dtype =
argument in the fromiter()
function is used to specify the data type we want the array to be returned as. In the code above, we parsed the int function to the argument, which made it return an array of integers. We could also parse float and bool functions to the argument to return an array of floating point values or boolean values.
Video, Further Resources & Summary
Do you need more explanations on how to convert a list into a NumPy array in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a list into a NumPy array in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these:
- Convert List to pandas DataFrame in Python (3 Examples)
- Access Dictionary within List in Python (Example)
- Convert List to Range in Python (2 Examples)
- Check if List of Lists is Empty in Python (2 Examples)
- Learn Python Programming
This post has shown how to convert a list into a NumPy array in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter