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:
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:

Convert Python list to numpy array using 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:
How simple is that! The output is:

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:
The output is:
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:
The output is:
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:
Here, we have simply updated the array2 construction to build from array1, instead of from the original list. The output is (again):
Note that nothing has changed. Let us make one final change and this time use np.asarray():
The output is (surprisingly):
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.
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.

Join our mailing list

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

Copyright @ Kodeclik 2022. All rights reserved.