How to concatenate all elements of list into string in Python

In this Python tutorial, we will understand the implementation of Python concatenate list to string. Here we will discuss how to concatenate various list elements into a string in Python.

In Python, we can concatenate list elements into a string using the following 5 methods

  • Using for loop & plus (+) operator
  • Using join() method
  • Using join() & list comprehension
  • Using join() & map() function
  • Using reduce() method

Python concatenate list to string

So, in this section, we will discuss all 5 methods to concatenate all list elements to a string in Python.

However, let us first start with the basic method of using the + operator.

Method 1: Python concatenate list to string using for loop & plus (+) operator

In this section, we will discuss the native approach to concatenating a Python list to a string.

So, in this method, first, we will use the for loop to iterate over each item given in the list. After this, we will use the + operator with the string variable and list elements to form a string using it.

Here is an example of this approach in Python.

# Defining a list
usa_info = ['The', 'United States', 'is a', 'federal republic',
            'consisting of', '50 states']

# Defining a empty string
info = ''

# Using for loop
for element in usa_info:
    info = info + element + ' '
    
# Printing final string
print(info)

In this example, we have defined a list containing multiple string-type values. After this, we utilized the for loop to iterate over each list element. In the last, we used the + operator to concatenate all the list elements to form a single string.

Here is the final result of the above Python program.

Output: The United States is a federal republic consisting of 50 states

Read: Linked Lists in Python

Method 2: UPython concatenate list to string using the join() method

Another way to concatenate a list to a string is by using the join() method in Python. The join() method in Python is a string method that allows joining all the string elements together from a list using a given separator.

Let us see an example where we will concatenate the list elements using the join() method in Python.

# Defining a list
usa_info = ['The', 'USA', 'is a country', 
            'located in', 'North America']


# Using join() to concatenate list to string
info = ' '.join(usa_info)
    
# Printing final string
print(info)

In this example, we were joining the list elements of the usa_info list using the join() method. However, as a separator, we have used an empty string (” “).

Here is the final result of the above Python program.

Output: The USA is a country located in North America

Read: Python Extend Vs Append

Method 3: Python concatenate list to string using list comprehension

List comprehension in Python is a concise way to create a new list by performing some operation on each item of an existing Python list.

However, we can use this list comprehension method with the join() method to concatenate all the list elements into a string.

Here is an example of this task in Python.

# Defining a list
usa_info = ['The', 'United States', 'is a', 'federal republic',
            'consisting of', '50 states']

# Using join() and list comprehension
info = ' '.join(str(item) for item in usa_info)
    
# Printing final string
print(info)

In the example, we utilized the list comprehension method within the join() method to get each list element. And after this, the join() method will concatenate each element together with an empty space.

After execution, we will get the following result.

Output: The United States is a federal republic consisting of 50 states

Read: How to find smallest number in a Python list

Method 4: Python concatenate list to string using the map() function

The map() is a built-in Python function that allows to execute a particular function on each element of a given iterable.

So, by using the map() function, we will convert each list element to a string data type. And then we will use the join() method to concatenate all the elements to form a single string.

Here is an example.

# Defining a list
usa_info = ['The', 'USA', 'is a country', 
            'located in', 'North America']


# Using join() & map() to concatenate list to string
info = ' '.join(map(str, usa_info))
    
# Printing final string
print(info)

In the example, we converted the usa_info list elements to string using the map() function. After this, we used the join() method to concatenate all list elements to a string in Python.

Here is the result of the above Python program.

Output: The USA is a country located in North America

Read: How to Reverse a List in Python

Method 5: Python concatenate list to string using reduce() method

The reduce() is a built-in Python function that is available in the functools module. This function enables us to apply binary functions to each element of an iterable resulting in reducing the result to a single value.

Here is a method to use the reduce() function with lambda and form a single string as a result.

# Importing reduce() method
from functools import reduce

# Defining a list
usa_info = ['America', 'is often', 'used as a', 
            'shorthand term', 'for the', 'USA']

# Using reduce() to concatenate list to string
info = reduce(lambda element_x, element_y: element_x + ' ' + element_y, usa_info)
    
# Printing final string
print(info)

In the example, we utilized the reduce() function to concatenate list elements of the usa_info list to the info string.

Once the above Python program is executed, we will get the following result.

Python concatenate list to string
Concatenating list of elements to string in Python

You may also like to read the following Python tutorials.

Conclusion

So, in this Python tutorial, we understood how Python concatenate list to string using 5 different methods. Moreover, we have also covered examples related to each method in Python.

Here is the list of methods that we discussed.

  • Using for loop & plus (+) operator
  • Using join() method
  • Using join() & list comprehension
  • Using join() & map() function
  • Using reduce() method