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: Usinglist 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 liststest_list1=["gfg","is","best"]test_list2=["love","CS"]# printing original listsprint("The original list 1 is : "+str(test_list1))print("The original list 2 is : "+str(test_list2))# forming pairstemp=[(a,b)foraintest_list1forbintest_list2]# performing concatenationres=[x+' '+yfor(x,y)intemp]# printing resultprint("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 : Usingproduct() + 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 comprehensionfromitertoolsimportproduct# initializing liststest_list1=["gfg","is","best"]test_list2=["love","CS"]# printing original listsprint("The original list 1 is : "+str(test_list1))print("The original list 2 is : "+str(test_list2))# concatenation using formatting and pairing using productres=['%s%s'%(ele[0],ele[1])foreleinproduct(test_list1,test_list2)]# printing resultprint("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()fromitertoolsimportproduct# initializing liststest_list1=["gfg","is","best"]test_list2=["love","CS"]# printing original listsprint("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 resultprint("The paired combinations : "+str(res))
OutputThe 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
Python3importitertools# initializing liststest_list1=["gfg","is","best"]test_list2=["love","CS"]# forming pairstemp=itertools.product(test_list1,test_list2)# performing concatenationres=[' '.join(pair)forpairintemp]# printing resultprint("The paired combinations : "+str(res))
OutputThe 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.