Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

How to convert a Python list to a numpy array

Suppose you are given a Python list, like [1,2,3,4,5] and desire to convert it to a numpy array. A trivial way to do it (and not a very pythonic way at that) is:

import numpy as np

mylist = [1,2,3,4,5]
myarray = np.array([])
for x in mylist:
  myarray = np.append(myarray,x)
print(myarray)

Here we have our list (mylist). We initialize a numpy array (myarray). Then we traverse the list and incrementally append each element into myarray. Note that np.append() by itself does not modify its first argument (i.e., myarray) but simply returns a copy of the array with the appended elements. We assign that to myarray to over-write it inside the loop.

The output is:

[1. 2. 3. 4. 5.]

Convert Python list to numpy array using numpy.array()

Convert Python list to numpy array

A very simple (and better) way to achieve our purpose is to simply pass the list as the argument to numpy.array() when we use it first, like this:

import numpy as np

mylist = [1, 2, 3, 4, 5]
myarray = np.array(mylist)
print(myarray)

How simple is that! The output is:

[1 2 3 4 5]

Convert Python list to numpy array using numpy.asarray()

A second way to convert a Python list to numpy array is using the numpy.asarray() function, like so:

import numpy as np

mylist = [1, 2, 3, 4, 5]
myarray = np.asarray(mylist)
print(myarray)

The output is:

[1 2 3 4 5]

We obtain the same result. What is the big difference? Why do we need two separate functions? The difference lies in the semantics of the conversion (or copying).

What is the difference between Python numpy array() and asarray()?

The subtle difference is that numpy.array() will create a new array by default whereas numpy.asarray() will not create a new array by default. To understand this distinction, let us create two arrays with the same values and experiment as shown below:

import numpy as np

mylist = [1, 2, 3, 4, 5]

array1 = np.array(mylist)
array2 = np.array(mylist)
print("List: " + str(mylist))
print("np.array: " + str(array1))
print("np.asarray: " + str(array2))
print()

mylist[0] = 5
array1[0] = 6
array2[0] = 12
print("List: " + str(mylist))
print("np.array: " + str(array1))
print("np.asarray: " + str(array2))

The output is:

List: [1, 2, 3, 4, 5]
np.array: [1 2 3 4 5]
np.asarray: [1 2 3 4 5]

List: [5, 2, 3, 4, 5]
np.array: [6 2 3 4 5]
np.asarray: [12  2  3  4  5]

As you can see, the list and the two arrays are all three separate entities so that modifying one does not modify the other.

Let us modify the program slightly so that array2 is really built on top of array1:

import numpy as np

mylist = [1, 2, 3, 4, 5]

array1 = np.array(mylist)
array2 = np.array(array1)
print("List: " + str(mylist))
print("np.array: " + str(array1))
print("np.asarray: " + str(array2))
print()

mylist[0] = 5
array1[0] = 6
array2[0] = 12
print("List: " + str(mylist))
print("np.array: " + str(array1))
print("np.asarray: " + str(array2))

Here, we have simply updated the array2 construction to build from array1, instead of from the original list. The output is (again):

List: [1, 2, 3, 4, 5]
np.array: [1 2 3 4 5]
np.asarray: [1 2 3 4 5]

List: [5, 2, 3, 4, 5]
np.array: [6 2 3 4 5]
np.asarray: [12  2  3  4  5]

Note that nothing has changed. Let us make one final change and this time use np.asarray():

import numpy as np

mylist = [1, 2, 3, 4, 5]

array1 = np.asarray(mylist)
array2 = np.asarray(array1)
print("List: " + str(mylist))
print("np.array: " + str(array1))
print("np.asarray: " + str(array2))
print()

mylist[0] = 5
array1[0] = 6
array2[0] = 12
print("List: " + str(mylist))
print("np.array: " + str(array1))
print("np.asarray: " + str(array2))

The output is (surprisingly):

List: [1, 2, 3, 4, 5]
np.array: [1 2 3 4 5]
np.asarray: [1 2 3 4 5]

List: [5, 2, 3, 4, 5]
np.array: [12  2  3  4  5]
np.asarray: [12  2  3  4  5]

Note that even though we made a change to array2 to have 12 as the first element that change is now reflected in array1 as well! In other words when i) numpy.asarray() is used, ii) the input to numpy.asarray() is another numpy array, the copies are made by reference by default so that changes in one will be reflected in another. This is a subtle change and the default behavior of numpy.asarray() (It is possible to over-ride this behavior with options in numpy.asarray() but this is beyond the scope of this blogpost). You should keep this in mind as without this understanding, you might get into painful debugging sessions.

To summarize, if you wish to convert a Python list to a numpy array use either 1. numpy.array(), or 2. numpy.asarray(). The main difference between the two methods is that numpy.array() will create a new array by default whereas numpy.asarray() will not create a new array by default and thus changes in one array will be propagated to other arrays.

For more understanding of numpy arrays, checkout our blogpost on testing for equality between numpy arrays. Also see how to modify the dimensions of a numpy array.

For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.