﻿ How to Convert a List into an Array in Python with Numpy ﻿

# How to Convert a List into an Array in Python with Numpy

In this article, we show how to convert a list into an array in Python with numpy.

Many times you may want to do this in Python in order to work with arrays instead of lists.

This is because arrays lend themselves to mathematical operations in a way that lists don't.

With arrays, we can do numerous, almost unlimited, mathematical operations.

With lists, this isn't possible in the same way.

So how can lists be converted to arrays in Python with numpy?

This can be done by putting the list in the statement, np.array.

This is shown in the code below.

So in our code, we first have a list called list1.

We then show the contents of this list.

And just as another way to confirm that this is in fact a list, we use the type() function to show the type of data it is. The result is a list.

We then create an array called array1 and set it equal to, np.array(list1)

What this line does is it converts a list into an array.

Now if we call the variable name, you see the contents of the array.

As another way to confirm that is in fact an array, we use the type() function to check. Doing this, you can see that the data is in fact an array (numpy).

Now being that we changed the list to an array, we are now able to do so many more mathematical operations that we weren't able to do with a list.

That's the power with arrays with numpy. They give us so much more mathematical functionality that was not present with lists.

We can now do numerous mathematical operations, including addition, subtraction, multiplication, and division, as well as trigonometric functions, etc., so on, and so forth.

Related Resources