Convert Each Item in the List to String using Python
Last Updated :
24 Dec, 2024
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)
Similar Reads
Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Python | Convert string enclosed list to list Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passe
5 min read
Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
3 min read
Python - Convert String to List of dictionaries Given List of dictionaries in String format, Convert into actual List of Dictionaries. Input : test_str = ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 8}]"] Output : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 8}]] Explanation : String converted to list of dictionaries. Input : test_str = ["[{'G
4 min read
Python | Convert List of String List to String List Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isd
6 min read
Convert String Float to Float List in Python We are given a string float we need to convert that to float of list. For example, s = '1.23 4.56 7.89' we are given a list a we need to convert this to float list so that resultant output should be [1.23, 4.56, 7.89].Using split() and map()By using split() on a string containing float numbers, we c
2 min read