Numpy Array to a List

N

Numpy Array to a List

When working with data in Python, the Numpy library is a powerful tool that provides support for large, multi-dimensional arrays and matrices. These arrays are fundamental to scientific computing and data analysis in Python. However, there are times when it may be necessary to convert a Numpy array to a standard Python list. This can be useful for situations where a list is more appropriate for the task at hand, or when interacting with other libraries or modules that expect lists as input.

In this article, we will explore how to convert a Numpy array to a list in Python. We will provide code examples to demonstrate the process, along with the output for each example. This will help you understand the different ways in which you can convert a Numpy array to a list and use the resulting list in your Python code.

Code Examples of convert a Numpy array to a list

Example 1

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array with values [1, 2, 3, 4, 5] and then use the tolist() method to convert it to a list. The resulting list is [1, 2, 3, 4, 5].

Example 2

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Convert Numpy array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a 2D Numpy array with values [[1, 2, 3], [4, 5, 6]] and convert it to a list. The resulting list is [[1, 2, 3], [4, 5, 6]].

Example 3

import numpy as np

arr = np.array([1.5, 2.7, 3.8, 4.2, 5.1])

# Convert Numpy array to list with data type conversion
lst = arr.astype(int).tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array with floating-point values and convert it to a list after converting the data type to integers. The resulting list contains the integer values of the original array.

Example 4

import numpy as np

arr = np.zeros(5)

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array filled with zeros and convert it to a list. The resulting list contains zeros with floating-point values.

Example 5

import numpy as np

arr = np.arange(1, 9, 0.5)

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array using the arange() function with a step of 0.5 and convert it to a list. The resulting list contains values ranging from 1.0 to 8.5.

Example 6

import numpy as np

arr = np.array(["numpywhere.com", "geek-docs.com", "deepinout.com"])

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array of strings and convert it to a list. The resulting list contains the string values of the original array.

Example 7

import numpy as np

arr = np.array([True, False, True, False])

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array of boolean values and convert it to a list. The resulting list contains the boolean values of the original array.

Example 8

import numpy as np

arr = np.random.randint(1, 10, size=5)

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array with random integer values and convert it to a list. The resulting list contains the random integer values of the original array.

Example 9

import numpy as np

arr = np.linspace(1, 5, num=10)

# Convert Numpy  array to list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a Numpy array using the linspace() function with 10 evenly spaced values between 1 and 5, and convert it to a list. The resulting list contains the evenly spaced values.

Example 10

import numpy as np

arr = np.array([[1, 2], [3, 4], [5, 6]])

# Convert Numpy  2D array to nested list
lst = arr.tolist()

print(lst)

Output:

Numpy  Array to a List

In this example, we create a 2D Numpy array and convert it to a nested list. The resulting list is a nested list representation of the original 2D array.

Conclusion of convert a Numpy array to a list

In this article, we have explored how to convert a Numpy array to a list in Python. We have provided code examples demonstrating various scenarios and data types, along with the output for each example.

By following these examples, you can easily convert Numpy arrays to lists and use them in your Python code as needed. Converting Numpy arrays to lists can be useful for a wide range of applications, so it is important to understand how to perform this conversion efficiently.

Latest Articles

Popular Articles