Convert Python List to NumPy Arrays

Learn via video courses
Topics Covered

Overview

In this article, we will learn to convert a list to NumPy array in Python. In Python, a list is a linear data structure that has the flexibility to grow and shrink and can hold heterogeneous elements whose data type need not be specified. On the other hand, an array is a data structure that may hold uniformly distributed elements. The NumPy library is used to implement arrays in Python.

Introduction

A list in Python is a linear data structure that can contain elements of multiple data types like numeric, character logical values, etc., and can be created using "[]" brackets. In Python, arrays are vectors that contain elements of the same data type and need to be declared by using a specific function like numpy.array(). Numpy is an open-source library for Python that can store multidimensional arrays. The Numpy library is used to convert a list to a numpy array.

Methods for Converting Lists to Arrays in Python

So now that we have conquered the basics let’s dive into the core of our article and how to use them, that is, to convert list into NumPy arrays.

First, let’s see how to use lists.

Ouput:

Here we defined and created a list called "items" and appended elements to it. Also, we can see that Python refers to this data type as a list.

Convert Lists to NumPy Arrays

Let's look at how we can convert a list to a NumPy array.

To do this, we make use of two functions from the NumPy package, namely, array() and asarray().

Let's see how we can use them to convert lists to NumPy arrays.

Note: Throughout the code snippets, an alias for numpy as np will be used to avoid repeatedly using the keyword numpy over and over again. This saves time and creates a clean code structure.

  • Using numpy array()

Output:

The list 'arr' was converted into a NumPy array using the asarray() function.

  • Using numpy asarray()

Output:

The list 'arr' was converted into a NumPy array using the asarray() function.

Let's dive a little deep into these functions.

  • array()

Syntax :

Let's look at each of these terms individually.

  • object (mandatory): An array, input lists, or tuples. If the input is received as a scalar, a 0-dimensional array will be returned.

  • dtype (optional): refers to the data type the input is to be converted into, e.g., int8, int32, float64, etc.

  • copy (optional): It takes a boolean variable as input, and if the bool variable is True, a copy of the array is made. If the bool variable is False, a reference to the original array is taken. True is taken as the default argument if this argument is not explicitly given.

    More arguments and details are available in the official NumPy documentation.

    Let's see array() in action.

Output:

The list 'arr' was converted into a NumPy array using the array() function.

We can see that initially, Python referred to ‘arr’ as a list data type, but when the array() function was used, Python now refers to it as a NumPy array. It refers to it as numpy.ndarray, which essentially means an n-dimensional array created from the NumPy package, and thus, we converted a list to a NumPy array.

  • asarray()

Syntax :

Input: n-dimensional lists or tuples output: n-dimensional array in numpy

Let's look at each of these terms individually.

  • object (mandatory): An array, input lists, or tuples. If the input is received as a scalar, a 0-dimensional array will be returned.

  • dtype (optional): refers to the data type the input is to be converted into, e.g., int8, int32, float64, etc.

    More arguments and details are available in the official NumPy documentation.

Let's see how to use asarray().

Output:

The list 'arr' was converted into a NumPy array using the array() function.

If both array() and asarray() do the same thing, then why is there a need for two of them? This is a question that might arise in your mind.

Let's tackle this question together.

The main difference between np.array() and np.asarray() is the copy flag, an argument which is False by default in the case of np.asarray()  and True by default in the case of np.array().

What this essentially means is that a copy of the input array is returned for np.array() whereas np.asarray() doesn't return one. Only a view is done. It leaves the input unchanged.

The term copy here refers to that it is a replica of the array, and changes made to it will not affect the original array.

The term view here refers to that it is a replica of the array, referenced under a different name, and changes made to it will affect the original array.

Output:

As we can see, the array arr_2 was a copy made from the original array arr_1, and since arr_2 is a copy, changes made to arr_1 was not reflected in arr_2.

Now let's look at copy=False.

Output:

Here, when the argument copy was changed to False, the array arr_2 became a view of the original array arr_1. Hence, changes made to arr_1 was reflected in arr_2.

numpy.array() has the argument copy set to True by default and numpy.asarray() does not have a copy argument, but internally the copy flag is set to False, therefore numpy.asarray() will always create a view of the array.

Although these are the most commonly used functions, there are various other functions to convert lists into NumPy arrays in the NumPy package. You can see them in the official NumPy documentation

Now let's look at a brief overview of how the data types are converted during the conversion of lists to NumPy arrays.

DATA TYPE CONVERSION

For further information, refer to the NumPy Documentation.

Convert List of Lists to NumPy Array of Arrays

Till now, we have only dealt with single-dimensional inputs, but both np.array() and np.asarray() can take n-dimensional inputs as well.

This means it can take lists, tuples, lists of lists, or tuples of tuples as an input array.

Using a function in the NumPy package called **ndim()**allows us to view the dimensions of the array.

Note: The input list or tuple should only contain data of a uniform data type.

  • 0D Array

Output:

Here a scalar value was passed as input. Hence we called it a 0D array. But after passing through the np.array() function, the scalar is now treated as a numpy.ndarray.

Also, we can see that ndim() returned a value of 0, implying that it is a 0D vector or array. 


  • 1D Array

Output:

Here, a 1D array was passed as input. But after passing through the np.array() function, the input list is now treated as a numpy.ndarray.

Also, we can see that ndim() returned a value of 1, implying that it is a 1D vector or array. 


  • 2D Array

Output:

Here, a 2D array was passed as input. But after passing through the np.array() function, the input list is now treated as a numpy.ndarray.

Also, we can see that ndim() returned a value of 2, implying that it is a 2D vector/array. 


  • 3D Array

Output:

Here, a 3D array was passed as input. But after passing through the np.array() function, the input list is now treated as a numpy.ndarray.

Also, we can see that ndim() returned a value of 3, implying that it is a 3D vector/array. 

This can go on up to n-dimensional arrays.

Convert a List of Lists into a NumPy Array

  • Flatten To convert a Python list of lists into a 1D NumPy array, we can make use of np.flatten() to flatten the array into 1 dimension.

Flattening is a technique used to convert an n-dimensional array into a single-dimensional array.

Output:

  • Reshape To convert n-dimensional lists into NumPy arrays, we can make use of the np.reshape() function.

Reshaping is a technique used to convert an n-dimensional NumPy array into a NumPy array of specified dimensions.

While doing this, one needs to be aware of the row and column constraints, the number of elements in the list or array, and the resultant NumPy array.

Output:

Advantages of Using NumPy Arrays over Python Lists

We have seen the basic use cases of Python lists and Numpy arrays. Now let's understand why we use or prefer NumPy arrays over lists.

  • The reason is that lists are a collection of items with different data types, so each item is not stored in a contiguous memory location, so fetching and collectively using these items in computation takes time, whereas arrays, on the other hand, contain items with similar data types and are stored in contiguous memory locations, so computation on arrays is faster.
  • To sum it up, the reason why NumPy is used over lists is that it's faster, way faster, about 50 times faster than lists. It also has a clean code structure and is built on top of C++, which is considered to be one of the most efficient and fastest general-purpose programming languages.
  • An array consumes less memory and is more convenient to use than lists. NumPy arrays use much less memory to store data, and it provides the virtue of specifying the data types of the elements. This allows the code to be optimized even further.
  • NumPy is not just more efficient, it is also more - convenient. You get a lot of vector and matrix operations free of charge, which sometimes allows you to avoid unnecessary work and save time. And they are also efficiently implemented.

Therefore by converting lists to NumPy arrays we can make the computations on the data much faster.

Conclusion

Let's see what we have learned here,

  • An array is a collection of similar data referenced under a common name.
  • Lists are a collection of similar data referenced under a common name.
  • We can convert list to numpy array using the array and asarray() methods.
  • Methods for converting lists to NumPy arrays.
  • Methods to reshape NumPy arrays.
  • Numpy arrays are more efficient than Python lists.

See Also: