Convert list to string Python
In this post, you will learn different ways to convert a list to a string using the Python programming language.
While developing software, we may face some scenario where there may be a need to convert a list to a string. In Python, List is mostly used data structure, it contains any type of data objects with 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 a list. In Python, string is used for representing characters. Strings are immutable, indexable, iterable. There are several ways to convert a list to a string, like using list comprehension, map() method, join() method.
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. It provides a flexible way to create strings from iterable objects. It joins each element of an iterable list by a string separator and returns the concatenated string. Here is the syntax of join() -
string.join(iterable)
Here, iterable is the required parameter. It is an iterable object where all the returned values are strings. The following code is an example of join() function to convert a list to a string. It is considered a space as a separator.
def listToString(x):
# initialize an empty string
string = " "
# return string
return(string.join(x))
# list elements
list_x = ['Welcome', 'to', 'etutorialspoint', 'website']
print(listToString(list_x))
Output of the above code -
Welcome to etutorialspoint website
Using map() method
Python's inbuilt map() function applies a function on all the items of an iterator given as input and returns a map object of the results after applying the given function to each item. The iterator can be a list, tuple, dictionary or string. The syntax of map() function is -
map(fun, iter)
Here, fun is the function to execute for each item and iter is an iterable compulsory object.
list_x = ['He', 'sold', 2, 'tables', 'and', 10, 'chairs']
# using map function
listToString = ' '.join(map(str, list_x))
print(listToString)
Output of the above code -
He sold 2 tables and 10 chairs
Using list comprehension
A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It provides a concise way to create lists and uses square brackets ([])
# Python program to convert a list
list_x = ['I', 'got', 5, 'chocolates', 'and', 2, 'candies']
# using list comprehension
listToString = ' '.join([str(s) for s in list_x])
print(listToString)
Output of the above code -
I got 5 chocolates and 2 candies
List iteration process
In the list iteration process, we iterate over the given string using a for loop and add its element to the string variable.
# Function to convert list to string
def listToString(x):
# initialize an empty string
str1 = ""
# traverse in the string
for ele in x:
str1 += ele
# return string
return str1
# list elements
list_x = ['Hello',' Friend',', ','How',' are',' you','?']
# Printing list
print(listToString(list_x))
Output of the above code -
Hello Friend, How are you?
Related Articles
Convert list to dictionary Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword