Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python program to concatenate every elements across lists

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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”] 
Output : [‘gfg love’, ‘gfg CS’, ‘best love’, ‘best CS’] 
Explanation : All strings are coupled with one another. 
 

Method #1: Using list comprehension

In this, we form pairs with each using list comprehension and then perform task of concatenation using another list comprehension.

Python3




# Python3 code to demonstrate working of
# All elements concatenation across lists
# Using list comprehension
 
# initializing lists
test_list1 = ["gfg", "is", "best"]
test_list2 = ["love", "CS"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# forming pairs
temp = [(a, b) for a in test_list1 for b in test_list2]
 
# performing concatenation
res = [x + ' ' + y for (x, y) in temp]
 
# printing result
print("The paired combinations : " + str(res))

Output:

The original list 1 is : [‘gfg’, ‘is’, ‘best’] The original list 2 is : [‘love’, ‘CS’] The paired combinations : [‘gfg love’, ‘gfg CS’, ‘is love’, ‘is CS’, ‘best love’, ‘best CS’]

Time Complexity: O(n2) -> two for loops

Space Complexity: O(n)

Method #2 : Using product() + list comprehension

In this, we perform task of forming combination using product() and list comprehension performs the task of concatenation.

Python3




# Python3 code to demonstrate working of
# All elements concatenation across lists
# Using product() + list comprehension
from itertools import product
 
# initializing lists
test_list1 = ["gfg", "is", "best"]
test_list2 = ["love", "CS"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# concatenation using formatting and pairing using product
res = ['%s %s' % (ele[0], ele[1]) for ele in product(test_list1, test_list2)]
 
# printing result
print("The paired combinations : " + str(res))

Output:

The original list 1 is : [‘gfg’, ‘is’, ‘best’] The original list 2 is : [‘love’, ‘CS’] The paired combinations : [‘gfg love’, ‘gfg CS’, ‘is love’, ‘is CS’, ‘best love’, ‘best CS’]

Time Complexity: O(n2) -> time complexity of product is O(n) and a for loop, O(n2)

Space Complexity: O(n)

Method #3: Using map and join

In this approach, we use map function to perform the task of concatenation and then join the resultant strings.

Python3




# Python3 code to demonstrate working of
# All elements concatenation across lists
# Using map() and join()
from itertools import product
# initializing lists
test_list1 = ["gfg", "is", "best"]
test_list2 = ["love", "CS"]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# concatenation using map() and join()
res = list(map(' '.join, product(test_list1, test_list2)))
  
# printing result
print("The paired combinations : " + str(res))

Output

The original list 1 is : ['gfg', 'is', 'best']
The original list 2 is : ['love', 'CS']
The paired combinations : ['gfg love', 'gfg CS', 'is love', 'is CS', 'best love', 'best CS']

Time Complexity: O(n2) -> time complexity of product is O(n) and a map function call O(n)

Auxiliary Space: O(n)

Method #4: Using itertools.product() and str.join()

  • Import the itertools module, which provides various functions to work with iterators.
  • Initialize two lists test_list1 and test_list2 with the desired elements.
  • Use itertools.product() function to form all possible pairs of elements from test_list1 and test_list2. This function returns an iterator that generates the pairs one at a time.
  • Store the iterator in a temporary variable called temp.
  • Iterate over the temp iterator and for each pair, join the elements using a space separator (‘ ‘) and append the result to a list called res.
  • Print the final result by converting the res list to a string using str() and passing it to the print() function

Python3




import itertools
 
# initializing lists
test_list1 = ["gfg", "is", "best"]
test_list2 = ["love", "CS"]
 
# forming pairs
temp = itertools.product(test_list1, test_list2)
 
# performing concatenation
res = [' '.join(pair) for pair in temp]
 
# printing result
print("The paired combinations : " + str(res))

Output

The paired combinations : ['gfg love', 'gfg CS', 'is love', 'is CS', 'best love', 'best CS']

Time complexity: O(n^2), where n is the length of the longer list (since we form all possible pairs).
Auxiliary space: O(n^2) for the temp list, since we need to store all the pairs.


My Personal Notes arrow_drop_up
Last Updated : 30 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials