In this Python tutorial, we will cover how to convert numpy array to a list of string in Python using the different methods or functions that exist in Python.
- How to convert numpy array to list of strings in Python using the astype()
- How to convert numpy array to list of strings in Python using vectorize()
- How to convert numpy array to list of strings in Python using a list comprehension
- How to convert numpy array to list of strings in Python using for loop
- How to convert numpy array to list of strings in Python using the map function
- How to convert numpy array to list of strings in Python using tolist()
How to convert numpy array to list of strings in Python
In Python, it is sometimes necessary to convert a NumPy array to a list of strings for various purposes such as while writing the file. Here we have discussed several methods to convert the numpy array to a list of strings in Python.
Method-1: Using the astype()
You can use the astype() method to convert the NumPy array to a string data type, and then use the tolist() method to convert it to a list of strings.
# import numpy library
import numpy as np
# create a NumPy array
arr = np.array([3, 5, 8, 7, 9])
# convert the NumPy array to a list of strings
lst = arr.astype(str).tolist()
# print the list
print(lst)
This code imports the NumPy library using the alias np, and creates a one-dimensional NumPy array called arr containing the values.
- Converts the NumPy array arr to a list of strings using the astype() method and tolist() method, and finally prints the resulting list lst.
Method-2: Using vectorize()
You can use the vectorize() method to apply the str() function to each element of the NumPy array, and then use the tolist() method to convert it to a list of strings.
# import numpy library
import numpy as np
# create a NumPy array
arr = np.array(['United Kingdom', 'United States', 'Canda', 'Brazil'])
# define a function to convert an element to a string
def convert_to_string(element):
return str(element)
# use the vectorize() method to apply the function to each element
vectorized_func = np.vectorize(convert_to_string)
string_arr = vectorized_func(arr)
# convert the NumPy array of strings to a list of strings
lst = string_arr.tolist()
# print the list
print(lst)
The above code converts a NumPy array of country names to a list of strings.
- It does this by defining a function that converts an element to a string and then using the np.vectorize() method to apply the function to each element of the array.
- The resulting array of strings is then converted to a list using the tolist() method and printed.
Output: ['United Kingdom', 'United States', 'Canda', 'Brazil']
Method-3: Using a list comprehension
This method involves creating a new list and using a list comprehension to loop through each element in the NumPy array, convert it to a string using the str() method, and then append it to the new list.
# import numpy library
import numpy as np
# create a numpy array
arr = np.array([11, 12, 13, 14, 15])
# convert numpy array to list of strings using list comprehension
str_list = [str(x) for x in arr]
# print the resulting list
print(str_list)
The above code imports the numpy library and creates a numpy array of integers.
- Then it uses a list comprehension to convert each element of the numpy array to a string and stores the resulting strings in a new list. Finally, it prints the resulting list of strings to the console
Method-4: Using for loop
This method uses for loop and adds the elements of the array using the append to the empty list as a list of strings.
# import numpy library
import numpy as np
# create a numpy array
arr = np.array(['United States', 'Canda', 'Brazil'])
# create an empty list to hold the string values
str_list = []
# loop through the numpy array and append the string values to the list
for element in arr:
str_list.append(str(element))
# print the resulting list
print(str_list)
The above code creates a numpy array of strings.
- It then initializes an empty list to hold the string values, and loops through the numpy array.
- Within the loop, each element in the array is converted to a string and then appended to the empty list using the append() method. Finally, the resulting list of strings is printed to the console using the print() function.
Output: ['United States', 'Canda', 'Brazil']
Method-5: Using the map function
You can also use the map() function to convert a NumPy array to a list of strings.
# import numpy library
import numpy as np
# create a numpy array
arr = np.array([23, 22, 31, 41, 15])
# use map() function to convert each element to a string and create a list
str_list = list(map(str, arr))
# print the resulting list
print(str_list)
The above code imports the NumPy library, and a NumPy array is created using the np.array() function.
- The map() function is used to apply the str() function to each element of the NumPy array, converting the integers to strings. The resulting sequence of strings is converted to a Python list using the built-in list() function.
- The resulting list of strings is assigned to the variable str_list. The print() function is used to print the contents of str_list.
Output: ['23', '22', '31', '41', '15']
Method-6: Using tolist()
You can use the tolist() method with map() and str() functions to convert the numpy array to a list of strings.
# import numpy library
import numpy as np
# create a numpy array
arr = np.array([11, 21, 13])
# convert numpy array to list of strings
lst = arr.tolist()
# convert each element to string
lst = [str(i) for i in lst]
# print the list
print(lst)
The above code demonstrates how to convert a numpy array to a list of strings in Python.
- First, numpy library is imported. Then, a numpy array arr is created using the np.array() function.
- Next, the ndarray.tolist() method is used to convert the numpy array arr to a list of integers called lst. However, since the elements of the list are still of type numpy data type, they need to be converted to strings to satisfy the requirement of the question.
- Therefore, a list comprehension is used to iterate over each element of lst and convert it to a string. The resulting list lst contains string elements.
Output: ['11', '21', '13']
Conclusion
In this Python tutorial, we have covered the 6 methods to convert the numpy array to a list of strings in Python using different examples.
- How to convert numpy array to list of strings in Python using the astype()
- How to convert numpy array to list of strings in Python using vectorize()
- How to convert numpy array to list of strings in Python using a list comprehension
- How to convert numpy array to list of strings in Python using for loop
- How to convert numpy array to list of strings in Python using the map function
- How to convert numpy array to list of strings in Python using tolist()
You may also like the following tutorials:
- How to Convert NumPy Tuple to List in Python
- Python NumPy Median() Function
- Python Copy NumPy Array
- Python NumPy Savetxt
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.