Python Program to Convert List into Array


What Is List?

Lists are constructed using square brackets. The list is the most powerful tool in Python because it does not have to be homogeneous. Integers, Strings, and Objects can all be found in the same list. Because lists are mutable, they can be changed even after they are formed.

One of the most important aspects of Python lists is their capacity to include duplicate values. This enables us to loop through the list's entries and find the value of each one. If the value needs to be replaced, we replace it.

There will be instances when you need to convert existing lists into arrays in order to perform certain operations on them (arrays enable mathematical operations on them in ways that lists do not).

A built-in method in Python allows us to convert lists into arrays is NumPy array.

What IS NumPY Array?

A data structure called a NumPy array can hold homogeneous elements. It is created using the NumPy module in Python. This library handles multi-dimensional arrays quite effectively.

Additionally, it effectively manages a huge amount of data pieces. List data structures use more memory than NumPy arrays. The index values can be used to distinguish between the list and the NumPy array. Additionally, it effectively manages a huge amount of data pieces. List data structures use more memory than NumPy arrays. The index values can be used to distinguish between the list and the NumPy array.

Use the NumPy library in Python to use an array. Type the command to install NumPy on your computer.

  • To create a NumPy array, use the NumPy.array() function.

  • To create an empty array, use the no.empty() function.

There will be times when you need to convert existing lists to arrays in order to make certain processes on them.

Converting Lists to Arrays in Python

The NumPy library provides two methods for converting lists to arrays in Python.

  • Using numpy.array()

  • Using numpy.asarray()

numpy array − It is a grid of identically-typed values that are each indexed by a nonnegative integer pair. The array's rank indicates the number of dimensions, and its shape is a tuple of numbers that represents the array's size along each dimension.

Numpy.asarray() − We utilise the asarray() function to turn input into an array. You can enter lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and arrays. The syntax is Numpy.asarray(arr, dtype=None, order=None).

Using NumPy.array() Method

Use Python's np.array() function to turn a list into an array. A list is passed as an argument to the NumPy library function np.array(), which returns an array containing every entry.

Example

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 in python, use the type() function.

import numpy as np
my_list = [11, 21, 19, 18, 29]
my_array = np.array(my_list)
print(my_array)

Output

[11 21 19 18 29]

UsingNumPy.asarray() Method

The NumPy library function np.asarray() takes a list as an argument, creates an array from it, and returns it. It invokes the NumPy.array() functions and the np.array() functions in accordance with the definite of the NumPy.asarray() functions.

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

The primary difference between numpy.array() and numpy.asarray() is that the copy flag is false in NumPy.asaaray() and True (by default) in NumPy.array().

Example

In the following program, a list of five numbers is created. This list is then converted into an array using the NumPy library's asarray() function.

import numpy as np
list_1 = [11, 21, 19, 18, 29]
array_1 = np.asarray(list_1)
print(array_1)

Output

[11 21 19 18 29]

Updated on: 24-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements