Python program to concatenate all Elements of a List into a String


List is one of mutable data structure available in python which is used to store the data of any datatype. It is denoted with the square braces "[]" and all the elements in the list are separated by comma. When we want to access an element from the list, indexing will be applied.

In the same way we have the string data structure which is immutable and stores the data in the string datatype. The string is given as double quotes or single quotes. The indexing will be applied to access the elements from the string.

Now in this article we will combine all the elements of a list into a single string. There are multiple approaches available in python, let’s go through each one in detail.

Using join() method

The join() method, which is a string method in Python. It takes an iterables such as list, tuple etc. and concatenates all its elements into a single string. We specify the separator ' ' to be used between each element during the concatenation.

Example

In this example we are trying to concatenate the list of elements ['Hello', 'Welcome', 'to', 'Tutorialpoints'] into a string by using the join() method. The join() method takes the list of elements as the input parameter and then returns the concatenated output.

my_list = ['Hello', 'Welcome', 'to', 'Tutorialspoint']
result = ' '.join(my_list)
print("The concatenated output:",result)

Output

The concatenated output: Hello Welcome to Tutorialspoint

Using loops

In this approach, we iterate over each element in the list and concatenate them with the desired separator space using the += operator. We also add a space after each element to separate them. Finally, we use the strip() method to remove any leading or trailing spaces from the resulting string.

Example

my_list = ['Hello', 'Welcome', 'to', 'Tutorialspoint']
result = ''
for item in my_list:
   result += item + ' '
print("The concatenated output:",result.strip())

Output

The concatenated output: Hello Welcome to Tutorialspoint

Using list comprehension and join()

In this approach, we use list comprehension and write the logic to iterate over each element in the list and create a new list with the same elements. We then use the join() method to concatenate all the elements in the new list into a single string.

Example

my_list = ['Hello', 'Welcome', 'to', 'Tutorialspoint']
result = ' '.join([item for item in my_list])
print("The concatenated output:",result)

Output

The concatenated output: Hello Welcome to Tutorialspoint

Using the reduce() function from the functools module

In this approach, we use the reduce() function from the functools module, which allows us to apply a function cumulatively to the items of an iterable. We use a lambda function that concatenates the current item with a space and the previous result. The reduce() function applies this lambda function to all elements of the list, resulting in the concatenation of all elements into a single string.

Example

my_list = ['Hello', 'Welcome', 'to', 'Tutorialspoint']
from functools import reduce
result = reduce(lambda x, y: x + ' ' + y, my_list)
print("The concatenated output:",result)

Output

The concatenated output: Hello Welcome to Tutorialspoint

Updated on: 02-Aug-2023

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements