NumPy array to a List of Strings in Python [5 Methods]

Do you want to convert a NumPy array to a string list? In this Python NumPy tutorial, I will explain how to convert a NumPy array to a list of strings in Python using different methods with some illustrative examples.

To convert a NumPy array to a list of strings in Python, we can use astype(str) to convert the entire array into strings and tolist() then change it into a list. List comprehension that iterates over each array element and explicitly converts it to a string. The map() function can be applied with str to each element, converting them efficiently. Lastly, np.vectorize(str) creates a vectorized string conversion function, useful for large arrays, and tolist() finalizes the conversion to a list.

Methods to convert a NumPy array to a list of strings in Python

There are five different methods to convert a NumPy array to a list of strings in Python:

  1. The astype(str) Method
  2. Using List Comprehension
  3. Using the map() Function
  4. Using a For Loop
  5. Using Vectorized Operations with NumPy

Let’s see them one by one using some demonstrative examples:

Note: To confirm the output is a list of strings, we will use all(isinstance(item, str) for the item in list_of_strings). The isinstance(item, str) function returns True if the item is a string in Python, and False otherwise. The all() function then checks if all these returned values are True, indicating every item in the list is a string, and returns True only if this condition is met for all elements in Python.

1. NumPy array to string list using astype(str) function

The astype(str) function in Python converts the entire NumPy array into an array of strings using and then converts this array to a list with the tolist() method.

Let’s see how the astype(str) can be used to convert a NumPy array to a list of strings in Python.

import numpy as np

state_codes = np.array([1, 2, 4, 5, 6])
list_of_strings = state_codes.astype(str).tolist()
print(list_of_strings)
print(all(isinstance(item, str) for item in list_of_strings))

Output:

['1', '2', '4', '5', '6']
True

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

convert a NumPy array to a list of strings in Python

2. NumPy array to list of strings using list comprehension

Creating a new list by iterating over each element of the NumPy array and applying the str function to convert each element to a string in Python.

This way we can convert a NumPy array to a list of strings in Python using list comprehension:

import numpy as np

zip_codes = np.array([90210, 10001, 60601, 73301, 33101])
list_of_strings = [str(zip_code) for zip_code in zip_codes]
print(list_of_strings)
print(all(isinstance(item, str) for item in list_of_strings))

Output:

['90210', '10001', '60601', '73301', '33101']
True

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

numpy array to string list in Python

3. Python array to list of strings using the map() function

This method applies the str function to every element of the array using the map() function, effectively converting each element to a string and then converting the result to a list.

This way the map() function can be used to convert a NumPy array to a list of strings in Python:

import numpy as np

area_codes = np.array([212, 213, 312, 415, 305])
list_of_strings = list(map(str, area_codes))
print(list_of_strings)
print(all(isinstance(item, str) for item in list_of_strings))

Output:

['212', '213', '312', '415', '305']
True

Upon running the code in Pycharm, the resulting output is captured in the screenshot provided below.

numpy array to list of strings in Python

4. Convert NumPy array to list of strings using for loop

Using a for loop involves iterating through the array with a for loop, converting each element to a string, and appending it to a new list.

This way we can use the for loop with the append() function to convert a NumPy array to a list of strings in Python:

import numpy as np

highways = np.array([66, 101, 1, 20, 40])

list_of_strings = []
for highway in highways:
    list_of_strings.append(str(highway))

print(list_of_strings)
print(all(isinstance(item, str) for item in list_of_strings))

Output:

['66', '101', '1', '20', '40']
True

Below is a screenshot showcasing the result, following the implementation of the code in the Pycharm editor.

python array to list of strings

5. NumPy array to list of strings using np.vectorize() function

We can use the NumPy vectorize function to create a vectorized version of the str function, which is then applied to the array to convert a NumPy array to a list of strings in Python.

import numpy as np

presidents = np.array([1, 16, 32, 44, 45])
vfunc = np.vectorize(str)
list_of_strings = vfunc(presidents).tolist()
print(list_of_strings)
print(all(isinstance(item, str) for item in list_of_strings))

Output:

['1', '16', '32', '44', '45']
True

Following the execution of the code within the Pycharm editor, a screenshot of the outcome is displayed below.

convert numpy array to list of strings in Python

Conclusion

Each of these methods—astype(str) with tolist() function, list comprehension, map(), and np.vectorize(str) with tolist() function provides a reliable and efficient way to convert a NumPy array to a list of strings in Python.

The choice among them can be based on factors like array size, performance requirements, and code readability, allowing flexibility to suit different programming needs and scenarios.

You may also like to read: