Site icon Spark By {Examples}

Convert Python List to NumPy Arrays

python list numpy array

How to convert a Python list to a Numpy array? The NumPy library is widely used in Python for scientific computing and working with arrays. NumPy provides a powerful array object called ndarray, which allows efficient storage and manipulation of homogeneous data.

You can convert a Python list to a NumPy array using many ways, for example, by using the numpy.array(), and numpy.asarray() methods. In this article, I will explain how to convert a Python list to Numpy array by using all these methods with examples.

1. Quick Examples of Converting Python List to NumPy Arrays

If you are in a hurry, below are some quick examples of how to convert lists to NumPy arrays.


# Quick examples of converting list to numpy arrays

import numpy as np

# Initialize the list
mylist = [2, 4, 6, 8, 10]

# Example 1: Convert python list to Numpy array
# Using numpy.array() method
arr = np.array(mylist)

# Example 2: Convert python list to numpy array
# Using numpy.asarray() method
arr = np.asarray(mylist)

# Example 3: Using numpy.asarray() method
arr = np.asarray(mylist)

# Made another array out of arr 
# using asarray function  
arr1 = np.asarray(arr)

# Change made in arr1
arr1[3] = 15

2. Using numpy.array() Method

You can use the np.array() method to convert a list to a NumPy array. For example, you can import the Numpy module and create a list called mylist. Then, you can use the np.array() method to convert the list into a NumPy array and assign it to the variable arr. Finally, you can print the resulting NumPy array.

NumPy arrays are a powerful data structure for numerical computations in Python, providing efficient and convenient operations on large datasets.

Note: The NumPy array representation is slightly different from the list representation. NumPy arrays are printed without commas between the elements.


import numpy as np

# Initialize list
mylist = [2, 4, 6, 8, 10]
print("Original list: ", mylist)

# Convert python list to numpy array
# Using numpy.array() method
arr = np.array(mylist)
print("After converting list to numpy array:", arr)

Yields below output.

python list numpy array

3. Using numpy.asarray() Method

The numpy.asarray() is one of the methods of the NumPy library that converts 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.

3.1 Syntax of numpy.asarray() Method

Following is the syntax of the numpy.asarray() method.


# Syntax of numpy.asarray() method
numpy.asarray(a, dtype=None, order=None)

3.2 Parameter of list numpy.asarray()

Let’s create a Python list called mylist containing five integers. After that, we can convert it into a NumPy array using a asarray() method.


import numpy as np

# Initialize list
mylist = [2, 4, 6, 8, 10]
print("Original list: ", mylist)

# Convert python list to numpy array
# Using numpy.asarray() method
arr = np.asarray(mylist)
print("After converting list to numpy array:", arr)

Yields the same output as above.

The key difference between numpy.array() and numpy.asarray() lies in how they handle the memory of the original object.


import numpy as np

# Initialize list
mylist = [2, 4, 6, 8, 10]
print("Original list: ", mylist)

# Convert python list to numpy array
# Using numpy.asarray() method
arr = np.asarray(mylist)
print("List to numpy array:", arr)

# Made another array out of arr 
# Using asarray function  
arr1 = np.asarray(arr)
print("arr1: " , arr1)
 
# Change made in arr1
arr1[3] = 15

print("mylist: " , mylist)
print("arr: " , arr)
print("arr1: " , arr1

# Output:
# Original list:  [2, 4, 6, 8, 10]
# List to numpy array: [ 2  4  6  8 10]
# arr1:  [ 2  4  6  8 10]
# mylist:  [2, 4, 6, 8, 10]
# arr:  [ 2  4  6 15 10]
# arr1:  [ 2  4  6 15 10]

4. Conclusion

In this article, I have explained how to convert a Python list to NumPy array by using numpy.array(), and numpy.asarray() methods with examples. Also explained the difference between np.array() and np.asarray() methods.

Happy Learning !!

Exit mobile version