How to Change Array to List Python

One of the great things about Python is how easy it is to expand. By using libraries like NumPy we can essentially combine Python’s ease of use with a large selection of additional math-based functionality. However, this sometimes brings about odd situations where we have multiple data types that are similar but not quite equivalent. Arrays and lists are one of the most common examples of this phenomenon.

A NumPy array and a standard Python list are similar in many respects. But converting between the two isn’t always a straightforward process. This is largely due to the fact that while an array and list are similar, they’re still not exactly the same thing. An ndarray, nested list and other permutations can make seemingly similar types very different. So how would we go about converting a NumPy array to list Python style? We can begin by looking at a simple example.

# array to list python
ourArray = numpy.array([1, 2, 3])
ourList = ourArray.tolist()

This example is probably quite a bit smaller than most people would expect. This is largely thanks to the power of the tolist function. We’ll soon see how this makes things considerably easier for any Python coder.

Remember to import NumPy with these examples. As Python lacks native array types we’ll always need to import numpy before working with them. Next, we use numpy to create an array. Then in the third line we perform the actual conversion of the array to a list and assign it to the ourList variable.

The tolist function can almost seem magical. It essentially takes a numpy data type and converts it into the nearest compatible Python type. The inout status is largely handled behind the scenes. If we called list instead of tolist then we’d instead have python scalars returned rather than an ordinary list.

But what would we do if we were working with something beyond a one dimensional array? The tolist function still has us covered. Again, the extent to which it’s able to convert between data types is extremely impressive. Let’s take a look at an example where we convert a two-dimensional array to a list.

ourArray = numpy.array([[1, 2, 3], [4, 5, 6]])
ourList = ourArray.tolist()

The method used to convert array types is fairly similar to what we saw before. And we can even expand this concept to handle full multi-dimensional arrays. Consider the following example.

ourArray = numpy.array([[8.3, 9.55], [10, 11.32], [12, 13.42]])
ourList = ourArray.tolist()
print(ourList)
print(type(ourList))

In this example, we see a multidimensional array consisting of floating point values. Many programming languages would require extensive conversion to move between data types in this situation. But as we’ll see, we can easily convert between array and list types even when dealing with multidimensional floats.

We begin by creating the array. We then convert it to a list using the tolist function. Note that there aren’t any special parameters to pass in this situation. The tolist function will automatically note the data types we’re working with. The end result of this conversion is stored in the ourList variable. We actually print that variable to screen in the next line. Note that the results are the same as the initial array. We find the only difference upon executing the next print statement.

When we call print for the final time it’ll be with the output of the type function. As the name suggests, this function basically just tells us what type a variable is an instance of. In this case, we’re passing the ourList variable. And the function will identify the python list structure. However, if we compare it to the initial array we can verify that it has the same data.

How to Change Array to List Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top