Python convert list to string
In this post, you will learn different ways to convert a list to a string using the Python programming language.
In the development process, we may face some scenarios where there may be a need to perform a data conversion operation. A List is the most commonly used data structure. It contains any type of data object with a comma separated and enclosed within a square bracket. A single list may contain data types like integers, strings, as well as objects or even another list inside another list. In Python, a string is used for representing characters. Strings are immutable, indexable, and iterable. There are several ways to convert a list to a string. Here, we have mentioned most of them.
Convert list to string using join() method
The join() method is a Python string method and returns a string by joining all the elements of an iterable, separated by a string separator.
# Python program to convert a list
# to string using join() function
# defining function
def listToString(l):
# initialize an empty string
string = " "
# return string
return (string.join(l))
# Driver code
list1 = ['apple','mango','kiwi','fig','guava']
print(listToString(list1))
Output of the above code:
apple mango kiwi fig guava
Convert list to string using join() and map() methods
In the given Python program, we have used the join() with the map() functions to convert a list to a string. The map() function converts elements in a list to a string because the join() method can only accept string elements.
# Python program to convert a list
# to a string using join() and map() function
list1 = ['cabbage','peas','beans','potato','tomato']
# using list comprehension
listToString = ' '.join(map(str, list1))
print(listToString)
Output of the above code-
cabbage peas beans potato tomato
Convert list to string using List comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. The join() method does not work if we have integer elements in the input list. But, we can overcome this failure by using the list comprehension along with the join() method. The list comprehension traverses the elements one by one, and the join() method concatenates the list's elements into a new string and returns them as output.
# Python program to convert a list
# to a string using join() and list comprehension
list1 = [11, 'apple', 8, 'mango', 21, 'orange']
# using list comprehension
listToString = ' '.join([str(element) for element in list1])
print(listToString)
Output of the above code-
11 apple 8 mango 21 orange
Convert list to string using enumerate function
The enumerate() is a predefined function in Python and is used to add a counter to an iteration and return it in the form of an enumerated object.
# Python program to convert a list
# to a string using enumerate() function
list1 = [11, 'guava', 8, 'kiwi', 15, 'fig']
# using enumerate
listToStr = ' '.join([str(elem) for i,elem in enumerate(list1)])
print(listToStr)
Output of the above code-
11 guava 8 kiwi 15 fig
Convert list to string using functools.reduce method
Python offers a function called reduce() that allows you to reduce a list in a more concise way. The reduce() is defined in "functools" module. It is used to apply a particular function passed in its argument to all of the list elements. Here, we have used the reduce() method with lambda function to convert a list to a string.
# Python program to convert a list
# to a string using functools.reduce() method
from functools import reduce
lst = ['pineapple',7,'apple',14,'pear',22,'orange',9]
listToStr = reduce(lambda a, b : a+ " " +str(b), lst)
print(listToStr)
Output of the above code-
pineapple 7 apple 14 pear 22 orange 9
Related Articles
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module