0
0
NumpyHow-ToBeginner ยท 2 min read

Numpy How to Convert List to Numpy Array

Use numpy.array(your_list) to convert a Python list to a numpy array, for example: np.array([1, 2, 3]).
๐Ÿ“‹

Examples

Input[1, 2, 3]
Output[1 2 3]
Input[[1, 2], [3, 4]]
Output[[1 2] [3 4]]
Input[]
Output[]
๐Ÿง 

How to Think About It

To convert a list to a numpy array, think of numpy as a tool that organizes data in a grid-like structure. You take your list, which is a simple collection of items, and pass it to numpy's array function. This function reads the list and creates a numpy array with the same elements, allowing you to use numpy's powerful features.
๐Ÿ“

Algorithm

1
Get the input list you want to convert.
2
Call the numpy.array() function with the list as the argument.
3
Store the result as a numpy array.
4
Return or use the numpy array as needed.
๐Ÿ’ป

Code

python
import numpy as np

my_list = [1, 2, 3]
my_array = np.array(my_list)
print(my_array)
Output
[1 2 3]
๐Ÿ”

Dry Run

Let's trace converting the list [1, 2, 3] to a numpy array.

1

Input list

my_list = [1, 2, 3]

2

Convert to numpy array

my_array = np.array(my_list) # creates array([1, 2, 3])

3

Print result

print(my_array) # outputs [1 2 3]

StepActionValue
1Input list[1, 2, 3]
2Convert to numpy arrayarray([1, 2, 3])
3Print result[1 2 3]
๐Ÿ’ก

Why This Works

Step 1: Why use numpy.array()

The numpy.array() function reads the list and creates a numpy array object that supports fast numerical operations.

Step 2: Preserves data order

The order and values in the list stay the same in the numpy array, so your data is unchanged but now in a powerful format.

Step 3: Enables numpy features

Once converted, you can use numpy's math functions, slicing, and broadcasting on the array.

๐Ÿ”„

Alternative Approaches

Using numpy.asarray()
python
import numpy as np
my_list = [1, 2, 3]
my_array = np.asarray(my_list)
print(my_array)
Similar to numpy.array(), but does not copy data if input is already an array, so it can be faster.
Using numpy.fromiter()
python
import numpy as np
my_list = [1, 2, 3]
my_array = np.fromiter(my_list, dtype=int)
print(my_array)
Creates array from iterable, requires specifying data type, useful for large iterables.
โšก

Complexity: O(n) time, O(n) space

Time Complexity

Converting a list to a numpy array requires reading each element once, so it takes linear time proportional to the list size.

Space Complexity

A new numpy array is created, so space used is proportional to the number of elements in the list.

Which Approach is Fastest?

Using numpy.asarray() can be faster if the input is already an array, as it avoids copying data.

ApproachTimeSpaceBest For
numpy.array()O(n)O(n)General list to array conversion
numpy.asarray()O(n) or O(1)O(n) or O(1)Avoiding copy if input is array
numpy.fromiter()O(n)O(n)Large iterables with known dtype
๐Ÿ’ก
Always import numpy as np and use np.array() to convert lists quickly.
โš ๏ธ
Forgetting to import numpy or misspelling np.array() causes errors.