How to convert List to string in Python

In this post, we are going to learn How to convert List to string in Python with code examples. Methods covered in this post, work for both lists of strings and lists of numbers.

1. Convert List to string using for-loop


Whenever it comes to converting a list to a string, the first approach is loop over, in which we will loop over each element of the list and concatenate. Using this, we are doing the below code to Convert a list to a string in Python. If we have a list of characters then we can use the for-loop to convert it to a string.

#Program to convert Python list to string 
animal_list = ['cat','bee','cat','dog','rat','lizard','spider']
list_to_str = ''
for element in animal_list:
  list_to_str+=  element
print("Converted python list to string:\n",
list_to_str)

Output

Converted python list to string:
 catbeecatdogratlizardspide

2. Convert list to string in Python Using Delimiter


We can convert the list to a string by adding some separators with the join() method. Let us see with an example of how to achieve the same, here we are using comma(,) as a separator between each list item.

#Program to convert Python list to string 

animal_list = ['cat','bee','cat','dog','rat']
separator = ','

converted_list = separator.join(animal_list)

print("Converted python list to string:\n",
converted_list)

Output

Converted python list to string:
 cat,bee,cat,dog,rat

3. Convert list to string In Python Using map()


The Python built-in function map() function takes two arguments one is function and Second is iterable(string,list,tuple,dictionary).The function is applied to each element of the iterable and then it returns the map() object. This technique works with both integer or string type list objects.

Syntax

map(function,iterable)

How is it working?

In this code example map() method takes two arguments str, iterable(animal_list) and str() function is executing on every element of the list to convert all elements to string. The list can have any type of element and finally, we join them using the join() method to convert the list to string in Python.

animal_list = ['cat','bee','cat','dog','rat','lizard','spider']
 
converted_list = ' '.join(map(string,animal_list )) 
print("Converted python list to string:\n",
converted_list)

Output

Converted python list to string:
 cat bee cat dog rat lizard spider

4. Convert list to string using List comprehension with .join()


List comprehension is a faster way instead of the loop over the list element in python. It creates a new list from the existing list by performing an operation or based on conditions over each element of the list.

In below the code snippet, we are using the list comprehension to convert a list to string by using the join() method. We are looping over each element of the list converting them to strings using str() method and later joining them using the join() method.

Syntax

#list comprehension syntax
[expression from item in list]

animal_list = ['cat','bee','cat','dog','rat',1,2,3]
 
converted_list = ' '.join([str(item) for item in animal_list])
print("Converted python list to string:\n",
converted_list)

Output

Converted python list to string:
 cat bee cat dog rat 1 2 3

5. Python Convert list to string using reduce()


the reduce() is the method is defined functools module.The reduce() takes two arguments function and iterable (tuple,list.dictionary).The function applied to each element of iterable. It does not return a new iterable instead returns a single value.

Syntax

(function, iterable [, initializer])
#Program to convert Python list to string 
import functools

animal_list = ['cat','bee','cat','dog','rat','lizard','spider']
 
seperator = ' '
strtolist = functools.reduce(lambda item_1,item_2 : item_1 + seperator + item_2, animal_list)
print("Converted python list to string:\n",
strtolist)

Output

Converted python list to string:
 cat bee cat dog rat lizard spider

5.1 Python Convert list of strings to int using reduce()


Sometimes the data list contains is int and we have to convert the list of strings to int. We can achieve the same using reduce() method by using the str() function

#Program to convert Python list to string 

import functools

animal_list = [1,2,56,90,100]
 
seperator = ' '
strtolist = functools.reduce(lambda item_1,item_2 : str(item_1) + seperator + str(item_2), animal_list)
print("Converted python list to string:\n",
strtolist)

Output

Converted python list to string:
 1 2 56 90 100

Conclusion

In this post, we have learned multiple ways how to Convert a list to string in Python.We can any of the above methods as per our requirement to convert a list to string in python.