Open In App

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

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, the task is to write a Python program to concatenate all elements in a list into a string i.e. we are given a list of strings and we expect a result as the entire list is concatenated into a single sentence and printed as the output.

Examples:

Input:  ['hello', 'geek', 'have', 'a', 'geeky', 'day']
Output: hello geek have a geeky day

Using the Naive approach to concatenate items in a list to a single string 

Here, we are taking a list of words, and by using the Python loop we are iterating over each element and concatenating words with the help of the “+” operator.

Python3




l = [ 'hello', 'geek', 'have',
     'a', 'geeky', 'day']
 
ans = ' '
for i in l:
   
  # concatenating the strings
  # using + operator
  ans = ans+ ' '+ i
   
print(ans)


Output:

hello geek have a geeky day

Using list comprehension to concatenate items in a list to a single string 

Here, we are using list comprehension to make the items into a Python string i.e. words into sentence. 

Python3




l = [ 'hello', 'geek', 'have',
     'a', 'geeky', 'day']
 
res = " ".join([str(item) for item in l])
res


Output:

'hello geek have a geeky day'

Using join() method to concatenate items in a list to a single string 

The join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string. 

Python3




# code
l = ['hello', 'geek', 'have',
   'a', '1', 'day']
 
# this will join all the
# elements of the list with ' '
l = ' '.join(l)
print(l)


Output:

hello geek have a 1 day

Using map() method to concatenate items in a list to a single string

The map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable. In map we passed the str as Datatype and list, this will iterate till the length of the list and join each element to form a string.

Python3




l = [ 'hello', 'geek', 'have',
     'a', 'geeky', 'day']
 
res = " ".join(map(str, l))
print(res)


Output:

hello geek have a geeky day

Using reduce() method to concatenate items in a list to a single string 

The reduce(fun, seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along. This function is defined in “functools” module.

Python3




from functools import reduce
 
l = ['hello', 'geek', 'have',
     'a', 'geeky', 'day']
 
# Concatenate all items in list to a string
res = reduce(lambda x, y: x + ' ' + y, map(str, l))
print(res)


Output:

hello geek have a geeky day

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)



Previous Article
Next Article

Similar Reads

Python | Concatenate N consecutive elements in String list
Sometimes, while working with data, we can have a problem in which we need to perform the concatenation of N consecutive Strings in a list of Strings. This can have many applications across domains. Let's discuss certain ways in which this task can be performed. Method #1: Using format() + zip() + iter() + list comprehensionThe combination of the a
8 min read
Python program to concatenate two Integer values into one
Given two integers a and b. The task is to concatenate these two integers into one integer. Examples: Input : a = 806, b = 91 Output : 80691 Input : a = 5, b = 1091 Output : 51091 Method 1: One method of achieving this can be counting the number of digits of second number. Then multiply the first number with 10^digits and adding both the numbers. B
2 min read
Python - Concatenate Rear elements in Tuple List
Given a tuple list where we need to concatenate rear elements. Input : test_list = [(1, 2, "Gfg"), ("Best", )] Output : Gfg Best Explanation : Last elements are joined. Input : test_list = [(1, 2, "Gfg")] Output : Gfg Explanation : Last elements are joined. Method #1 : Using list comprehension + join() In this, we check for last element using "-1",
6 min read
Python program to concatenate every elements across lists
Given 2 lists, perform concatenations of all strings with each other across list. Input : test_list1 = ["gfg", "is", "best"], test_list2 = ["love", "CS"] Output : ['gfg love', 'gfg CS', 'is love', 'is CS', 'best love', 'best CS'] Explanation : All strings are coupled with one another. Input : test_list1 = ["gfg", "best"], test_list2 = ["love", "CS"
4 min read
Python - Concatenate String values in Dictionary List
Sometimes, while working with Python records data, we can have a problem in which we require to perform concatenation of string values of keys by matching at particular key like ID. This kind of problem can have application in web development domain. Let's discuss certain way in which this task can be performed. Input : test_list = [{'id': 17, 'gfg
4 min read
Python - Concatenate Ranged Values in String list
Given list of strings, perform concatenation of ranged values from the Strings list. Input : test_list = ["abGFGcs", "cdforef", "asalloi"], i, j = 3, 5 Output : FGorll Explanation : All string sliced, FG, or and ll from all three strings and concatenated. Input : test_list = ["aGFGcs", "cforef", "aalloi"], i, j = 1, 4 Output : GFGforall Explanation
5 min read
Python - Concatenate Random characters in String List
Given a String list, perform concatenation of random characters. Input : test_list = ["Gfg", "is", "Best", "for", "Geeks"] Output : "GiBfe" Explanation : Random elements selected, e.g G from Gfg, etc.Input : test_list = ["Gfg", "is", "Best"] Output : "fst" Explanation : Random elements selected, e.g t from Best, etc. Method #1 : Using loop + random
6 min read
Python program to Concatenate Kth index words of String
Given a string with words, concatenate the Kth index of each word. Input : test_str = 'geeksforgeeks best geeks', K = 3 Output : ktk Explanation : 3rd index of "geeksforgeeks" is k, "best" has 't' as 3rd element. Input : test_str = 'geeksforgeeks best geeks', K = 0 Output : gbg Method #1 : Using join() + list comprehension + split() In this, we per
4 min read
Concatenate multiIndex into single index in Pandas Series
In this article, we will see how to concatenate multi-index to a single index in Pandas Series. Multi-index refers to having more than one index with the same name. Create a sample series: C/C++ Code # importing pandas module import pandas as pd import numpy as np # Creating series data for address details index_values = pd.Series([('sravan', 'addr
3 min read
Python - Concatenate consecutive elements in Tuple
Sometimes, while working with data, we can have a problem in which we need to find cumulative results. This can be of any type, product, or summation. Here we are gonna discuss adjacent element concatenation. Let’s discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression + tuple() The combination of a
4 min read