How to Convert List to Array in Python

There are the following methods to convert a list to an array in Python.

  1. np.array(): It takes a list and returns an array containing all the list elements.
  2. np.asarray(): It accepts a list, converts it into an array, and returns it.
  3. arr.array(): It accepts the list and data type and returns the array.

Method 1: Using the np.array() method

To convert a list to an array in Python, you can use the np.array() method. The function takes a list as an argument and returns an array containing all the list elements.

import numpy as np

elon_list = [11, 21, 19, 18, 29]
elon_array = np.array(elon_list)

print(elon_array)
print(type(elon_array))

Output

[11 21 19 18 29]
<class 'numpy.ndarray'>

In this example, we defined a list, which we converted into an array using the np.array() function and printed the array and its data type. To check variable data type, use the type() function.

Method 2: Using numpy.asarray() method

The np.asarray() is a numpy library function that takes a list as an argument converts it into an array, and returns it.

As per the definition of the numpy.asarray() function, it calls the numpy.array() function inside itself. So behind the scenes, the np.asarray() function calls the np.array() function.

def asarray(a, dtype=None, order=None):
  return array(a, dtype, copy=False, order=order)

The main difference between numpy.array() and numpy.asarray() is that the copy flag is False in the case of numpy.asarray() and True (by default) in the case of numpy.array().

import numpy as np

elon_list = [11, 21, 19, 18, 29]
elon_array = np.asarray(elon_list)

print(elon_array)
print(type(elon_array))

Output

[11 21 19 18 29]
<class 'numpy.ndarray'>

Method 3: Using the arr.array() method

You can use the array.array() method to convert a list to an array. To use the array() method from the array module, you need to import the array module and specify the data type and the list as arguments.

import array as arr

data_list = [11, 21, 19, 18, 29]
data_array = arr.array('i', data_list)
print(data_array)
print(type(data_array))

Output

array('i', [11, 21, 19, 18, 29])
<class 'array.array'>

np.array vs np.asarray

The main difference between np.array() and np.asarray() is that np.array() will create a duplicate of the original object, and np.asarray() will follow the changes in the original object.

For example, when a copy of the array is made using np.asarray(), the modifications made in one array would be reflected in the other array but don’t display the changes in the list from which an array is made. In the case of np.array(), this doesn’t happen.

FAQs

Why would I want to convert a list to an array in Python?

Converting a list to an array in Python can be helpful when performing mathematical operations on the list elements, as arrays are optimized for these operations.

Additionally, converting a list to an array can help to reduce memory usage and increase the performance of your code.

Can I convert a list of different data types to an array in Python?

No, all the elements in an array must have the same data type. If you have a list with elements of different data types, you must convert them to a common data type before converting the list to an array. This can be done using type-casting functions such as int, float, and str.

How does converting a list to an array affect the performance of my code?

Converting a list to an array in Python can significantly improve the performance of your code, especially if you are performing mathematical operations on the elements of the list.

Arrays are optimized for these types of operations and can be faster and more memory-efficient than lists.

Conclusion

The best and an efficient way to convert a list to an array in Python is to use the np.array() function.

The best way to convert an array to a list is to use the list() function in Python.

Further reading

Python list to a tuple

Python list to string

Python list to dataframe

Python list to json

Python list to csv

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.