How to convert a list to an array in Python

In Python, a list is a collection of elements that can be of any type, whereas an array is a collection of elements of the same type. Therefore, sometimes it may be necessary to convert a list to an array. You can use various methods to convert a list to an array in Python.

Using the array() function from the array module

The easiest way to convert a list to an array is to use the array() function from the array module in Python. The array() function creates an array of specified type and size from a Python list.

Following is the code to convert a list to an array using the array() function:

import array my_list = [1, 2, 3, 4, 5] my_array = array.array('i', my_list) print(my_array) #Output: array('i', [1, 2, 3, 4, 5])

In the above code, 'i' is the type code for integer values. The array() function takes two arguments - the first argument is the type code for the array, and the second argument is the Python list that needs to be converted.

Using the numpy module

Another way to convert a list to an array is to use the numpy module in Python. The numpy module provides support for multidimensional arrays and matrices.

Following is the code to convert a list to an array using the numpy module:

import numpy as np my_list = [1, 2, 3, 4, 5] my_array = np.array(my_list) print(my_array) #Output: [1 2 3 4 5]

In the above code, first import the numpy module and create a list of integers. Then use the array() function of numpy to convert the list to an array.

Using the fromlist() method of the array module

You can also use the fromlist() method of the array module to convert a list to an array. The fromlist() method creates an array from a Python list.

Following is the code to convert a list to an array using the fromlist() method:

import array my_list = [1, 2, 3, 4, 5] my_array = array.array('i') my_array.fromlist(my_list) print(my_array) #Output: array('i', [1, 2, 3, 4, 5])

In the above code, first import the array module and create a list of integers. Then create an empty array using the array() function and use the fromlist() method to populate the array with the elements of the list.

Using the append() method of the array module

You can also use the append() method of the array module to convert a list to an array. The append() method appends elements to the end of the array.

Following is the code to convert a list to an array using the append() method:

import array my_list = [1, 2, 3, 4, 5] my_array = array.array('i') for i in my_list: my_array.append(i) print(my_array) #Output: array('i', [1, 2, 3, 4, 5])

In the above code, first import the array module and create a list of integers. Then create an empty array using the array() function and use a for loop to append each element of the list to the end of the array.

Convert array of floating-point values

You can also use other data type codes to convert a list of elements to an array of different types. For example, if you have a list of floating-point values, you can use the data type code 'f' to convert it to an array of floating-point values:

import array my_list = [1.0, 2.0, 3.0, 4.0, 5.0] my_array = array.array('f', my_list) print(my_array) #Output: array('f', [1.0, 2.0, 3.0, 4.0, 5.0])

In the above code, create a list of floating-point values and use the data type code 'f' to convert it to an array of floating-point values.


How to convert python list to python array

Here are some of the commonly used data type codes for the array() function:

  1. 'b': signed integer of size 1 byte (values between -128 and 127)
  2. 'B': unsigned integer of size 1 byte (values between 0 and 255)
  3. 'i': signed integer of size 2 bytes (values between -32,768 and 32,767)
  4. 'I': unsigned integer of size 2 bytes (values between 0 and 65,535)
  5. 'f': floating-point value of size 4 bytes
  6. 'd': floating-point value of size 8 bytes