How to convert a list to an array in Python? You can convert a list to an array using the array module. This module provides an efficient way to store and manipulate homogeneous numeric data (elements of the same type) like integers, floats, etc. Unlike a list, an array allows you to define the type of data that can be stored in it, which can lead to faster operations and more memory efficiency.
You can convert a list to an array in Python using many ways, for example, by using the array(), np.array(), and numpy.asarray()functions. In this article, I will explain how to convert a list to an array by using all these functions with examples.
1. Quick Examples of Converting List to Array
If you are in a hurry, below are some quick examples of how to convert a list to an array.
# Quick examples of converting list to array
from array import array
import numpy as np
# Example 1: Convert list to Python array module
# Using array() + data type indicator
mylist = [2, 4, 6, 8, 10]
result = array(“i”, mylist)
# Example 2: Convert the list to an array of floats
mylist = [2.0, 4.0, 6.0, 8.0, 10.0]
result = array(“f”, mylist)
# Example 3: Using the np.array() function
mylist = [10, 20, 30, 40, 50]
myarray = np.array(mylist)
# Example 4: Convert the list to two-dimensional array
mylist = [[10, 20], [30, 40],[50,60]]
myarray = np.array(mylist)
# Example 5: Using the np.asarray() function
mylist = [10, 20, 30, 40, 50]
myarray = np.asarray(mylist)
2. Convert List to Array Using array module
You can use the built-in array() function provided by the array module to create an array from a list or a tuple. This is a convenient and efficient way to handle arrays in Python.
To create an array of integers using the array() function, you can use the i data type. Here, the first argument to the array() function is the data type indicator ‘i’, which indicates that the array will contain integers. The second argument is the input list mylist.
Similarly, you can use other data type indicators such as ‘f’ for floating-point numbers, ‘d’ for double-precision floating-point numbers, ‘u’ for Unicode characters, etc., depending on the type of data you want to store in the array.
from array import array
# Initialize list
mylist = [2, 4, 6, 8, 10]
print(“Original list: “, mylist)
# Convert list to Python array
# Using array module + data type indicator
result = array(“i”, mylist)
print(‘After converting the list to an array:’, result)
print(type(result))
Yields below output.
You can also convert the list to an array of floats. First, import the array module, then create a list of floats called mylist and print it. Next, Use the array() function from the array module to convert the list to an array of floats. The first argument to the array() function is the type code for the array, which in this case is ‘f’for floats.
from array import array
# Create a list of floats
mylist = [2.0, 4.0, 6.0, 8.0, 10.0]
print(“Original list: “, mylist)
# Convert the list to an array of floats
result = array(“f”, mylist)
print(‘After converting the list to an array:’, result)
print(type(result))
Yields below output.
3. Convert List to Array Using the NumPy Module
To create an array object in Python, you can use the np.array() function from the numpy module. It takes an iterable (such as a list or a tuple) as its argument and returns a new array with the same elements. In this example, you can import the NumPy library and create a list called mylist the elements [2, 4, 6, 8, 10]. Then, you can use the np.array() function to create a new array myarray with the same elements as mylist.
import numpy as np
mylist = [2, 4, 6, 8, 10]
print(“Original list: “, mylist)
# Using the np.array() function
myarray = np.array(mylist)
print(“After converting the list to an array : “, myarray)
print(“Type of data structure is :”,type(myarray))
# Output:
# Original list: [2, 4, 6, 8, 10]
# After converting the list to an array : [ 2 4 6 8 10]
# Type of data structure is : <class ‘numpy.ndarray’>
4. Convert the List to a Two-Dimensional Array
Alternatively, you can use np.array() function to convert a list to a two-dimensional array in Python. For example, Apply the np.array() function over the list of lists and create the 2-D array. The resulting array has three rows and two columns, with the elements of the original list arranged in a grid.
import numpy as np
mylist = [[2, 4], [6, 8],[10, 12]]
print(“Original list: “, mylist)
# Convert list to two-dimensional array
myarray = np.array(mylist)
print(“After converting list to two-dimensional array: n”, myarray)
# Output:
# Original list: [[2, 4], [6, 8], [10, 12]]
# After converting list to two-dimensional array:
# [[ 2 4]
# [ 6 8]
# [10 12]]
5. Convert List to Array Using numpy.asarray() Function
You can use numpy.asarray() which is another function of the numpy module, to convert a given input to an array. If the input is already an array, it returns the same array, but if the input is a list, tuple, or any other sequence-like object, it creates a new array with the same data.
4.1 Syntax of numpy.asarray() Function
Following is the syntax of the numpy.asarray() Function.
# Syntax of numpy.asarray() function
numpy.asarray(a, dtype=None, order=None)
4.2 Parameter of list numpy.asarray()
a – It is the input data. It can be any sequence-like object.
dtype – It is an optional parameter that specifies the data type of the output array. If not provided, NumPy will determine the data type automatically.
order – It is an optional parameter that specifies the memory layout of the output array. It can be “C” for C-style row-major layout, or “F” for Fortran-style column-major layout.
You can use the np.asarray() function to convert a Python list to a NumPy array. Let’s create a list and then apply the np.asarry() function to convert the array from the given list.
import numpy as np
mylist = [2, 4, 6, 8, 10]
print(“Original list: “, mylist)
# Using the np.asarray() function
myarray = np.asarray(mylist)
print(“After converting list to array : “, myarray)
# Output:
# Original list: [2, 4, 6, 8, 10]
# After converting list to array : [ 2 4 6 8 10]
Conclusion
In this article, I have explained how to convert a list to an array in Python by using array(), np.array(), and numpy.asarray() functions with examples.
Happy Learning !!
How to convert a list to an array in Python? You can convert a list to an array using the array module. This module provides an efficient way to store and manipulate homogeneous numeric data (elements of the same type) like integers, floats, etc. Unlike a list, an array allows you to define the type Read More Python, Python Tutorial, Pending review by admin, Python Array Examples, Python List Examples