Open In App

Convert Each Item in the List to String using Python

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Working with lists in Python is a common task, and there are various scenarios where you might need to convert each item in a list to a string. In this article, we will explore different methods to achieve this using list comprehension, the map() function, the enumerate function, and recursion.

Example :

Input : ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
Output : I want 4 apples and 18 bananas

How Can I Convert Each Item In The List To a String?

below, are the methods of How Can I Convert Each Item In The List To a String in Python

Convert Each Item In The List To a String using List Comprehension

In this example, the below code converts a mixed-type list ‘s’ into a string using list comprehension and the ‘join’ method. It prints the type of the original list’s third element. The resulting string is printed and the type of its third character is displayed.

Python3




# Python program to convert a list to string using list comprehension
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
  
# using list comprehension
listToStr = ' '.join([str(elem) for elem in s])
 
print(type(s[2]))
print(listToStr)
print(type(listToStr[2]))


Output

<class 'int'>
I want 4 apples and 18 bananas
<class 'str'>


Convert Each Item In The List To String using map() Function

In this example, below Python code converts a mixed-type list ‘s’ into a string using the ‘map’ function and ‘join’ method. It prints the type of the original list’s third element, the resulting string, and the type of its third character.

Python3




# Python program to convert a list to string map
   
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
  
# using map comprehension
listToStr = ' '.join(map(str, s))
  
print(type(s[2]))
print(listToStr)
print(type(listToStr[2]))


Output

<class 'int'>
I want 4 apples and 18 bananas
<class 'str'>


Convert Each Item In The List To String using enumerate function

In this example, below Python code converts a mixed-type list ‘s’ into a string using list comprehension with enumeration. It prints the type of the original list’s third element, the resulting string, and the type of its third character.

Python3




s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
  
listToStr = ' '.join([str(elem) for i,elem in enumerate(s)])
  
print(type(s[2]))
print(listToStr)
print(type(listToStr[2]))


Output

<class 'int'>
I want 4 apples and 18 bananas
<class 'str'>


Convert Each Item In The List To String using Recursion

In this example, below Python code defines a recursive function `list_to_string` to convert a mixed-type list ‘s’ into a string. It then prints the type of the original list’s third element, the resulting string from the recursive function, and the type of its third character.

Python3




def list_to_string(lst):
    if not lst:
        return []
    return [str(lst[0])] + list_to_string(lst[1:])
 
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']
listToStr = list_to_string(s)
 
print(type(s[2]))
print(listToStr)
print(type(listToStr[2]))


Output

<class 'int'>
['I', 'want', '4', 'apples', 'and', '18', 'bananas']
<class 'str'>


Conclusion

In conclusion, converting each item in a list to a string in Python can be achieved through various methods, each catering to different preferences and requirements. Whether using list comprehension for simplicity, the map() function for concise iteration, the enumerate function for incorporating indices, or recursion for a less conventional approach, Python provides versatile solutions for this common task.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads