Open In App

Convert Each Item in the List to String using Python

Last Updated : 24 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting each item in a list to a string is a common task when working with data in Python. Whether we're dealing with numbers, booleans or other data types, turning everything into a string can help us format or display data properly. We can do this using various methods like loops, the map() function, list comprehensions, join() and more.

Using map() Function

The map() function allows us to apply a function (in this case, str()) to each item in a list, making this method shorter and more efficient than using a loop. The map() function takes two arguments: the first is the function we want to apply and the second is the iterable . It applies the function to each item in the list and returns a map object, which we can then convert into a list using list().

Python
# Initial list with mixed data types
a = [1, 2, 3.5, True]

# Use map to convert each item to a string
b = list(map(str, a))

# Print the result
print(b)  

Output
['1', '2', '3.5', 'True']

Other methods of converting items in a list to strings are:

Using Loop

The most basic method to convert each item in a list to a string is by using a simple for loop. In this method, we create an empty list to store the converted string items. We then loop through each element in the list, convert it to a string using the str() function and append the result to the new list.

Python
# Initial list with mixed data types
a = [1, 2, 3.5, True]

# Create an empty list to store the string versions
b = []

# Loop through each item in the list and convert it to a string
for item in a:
    b.append(str(item))

# Print the result
print(b) 

Output
['1', '2', '3.5', 'True']

Using List Comprehension

List comprehensions are a more Pythonic way to achieve the same result as the map() function but in a single line of code. A list comprehension provides a compact way to loop through a list and apply a transformation to each item. In this case, we apply str() to each item in the list and create a new list with the string values.

Python
# Initial list with mixed data types
a = [1, 2, 3.5, True]

# Convert each item to a string using list comprehension
b = [str(item) for item in a]

# Print the result
print(b)  

Output
['1', '2', '3.5', 'True']

Using reduce()

The reduce() function from the functools module allows us to apply a function cumulatively to the items of a list. reduce() applies a function (in this case, a lambda function that concatenates strings with a space) to the items in the list. It processes the list from left to right, combining the items into a single result.

Python
from functools import reduce

# Initial list with mixed data types
a = [1, 2, 3.5, True]

# Use reduce to apply str() to each item and concatenate
b = reduce(lambda x, y: str(x) + ' ' + str(y), a)

# Print the result
print(b)  

Output
1 2 3.5 True

Next Article

Similar Reads