Convert 2D List to Array in Python (2 Examples)

 

Hi! This short tutorial will show you how to transform a 2D list to an array in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Example 2D List of Integer Values

Here, we will create the example Python 2D list of integer values that will be turned into an array in this tutorial.

So, in your preferred Python programming IDE, run the code below to create the example 2D list of integers:

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
print(my_list)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
print(type(my_list))
# <class 'list'>

With the example 2D list created, we will now explore ways to change it into an array.

For this tutorial, we will be making use of the Python NumPy library, which offers very simple functions, mostly one-liners, to turn a 2D list to an array.

NumPy is the foremost library in Python for numerical and scientific calculations. So, to use NumPy, we will need to install and import it like so:

# install NumPy
pip install numpy
 
# import NumPy
import numpy as np

Now, let use use some of its array-building functions.
 

Example 1: Transform 2D List to Array Using .array() Function

In this first example, we will use the NumPy .array() function to change the 2D list to an array:

my_array = np.array(my_list)
 
print(my_array)
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
 
print(type(my_array))
# <class 'numpy.ndarray'>

In this example, we used the np.array() function and parsed my_list as an argument. The np.array() function creates a new array object and copies the elements from the 2D list into the array.

Next, the resulting array is stored in the variable my_array, then the object and its data type are printed to the console using the print() function.
 

Example 2: Transform 2D List to Array Using .asarray() Function

In this next example, we will use NumPy’s .asarray() function to change the 2D list to an array:

my_array = np.asarray(my_list)
 
print(my_array)
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
 
print(type(my_array))
# <class 'numpy.ndarray'>

Here, the np.asarray() function creates a new array object, just like np.array(), and copies the elements from the 2D list into the array. The resulting array is stored in my_array, and its class is printed to verify the conversion.
 

Difference Between .array() Function & .asarray() Function

Having used both functions, you may wonder what exactly makes them different since they seem to work in the exact same way. Well, below are some differences between the two functions:

Array Function (np.array()):

  • When given an input argument that is a Python list, np.array() creates a new NumPy array by copying the data from the input list. The resulting array is independent of the original list.
  • If the input argument is already a NumPy array, np.array() creates a new copy of the array. This behavior ensures that the resulting array is a separate object from the original.

As Array Function (np.asarray()):

  • If the input argument is already a NumPy array, np.asarray() simply returns the input array as is, without making a copy. This behavior is useful when you want to ensure that an input is a NumPy array, but you don’t want to create a new copy if it already is.
  • If the input argument is a Python list or any other iterable, np.asarray() creates a new NumPy array by copying the data from the input. This behavior is the same as np.array() for iterable inputs.

In summary, np.array() is more versatile and always creates a new array, whereas np.asarray() avoids making a copy if the input is already a NumPy array.

 
So, with that, we have demonstrated, using 2 examples, how to convert a 2D list to an array in Python. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a 2D list to an 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 2D list to an 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 ones:

This post has shown how to convert a 2D list to an array in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Top