5 Ways to convert List to string in Python

Python List

In this post, we are going to learn 5 Ways 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 convert 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 list to string in Python

If we have list of character then we can use the for-loop to convert it to 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 by 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. Using map()

The Python map() function is built-in function takes two arguments one is function and iterable(string,list,tuple,dictionary).The function is applied to each element of 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.

#Program to convert Python list to string 

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.

Syntax

#list comprehension syntax
[expression from item in list]

How it is works?

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

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 string 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

These are ways to Python Convert list to string.We can any of above methods as per our requirement to convert list to string in python.