Python program to concatenate every elements across lists


In this article, we will learn a Python program to concatenate every element across lists.

Methods Used

The following are the various methods to accomplish this task −

  • Using the List Comprehension and String Concatenation

  • Using itertools.product() Function

Example

Assume we have taken two input lists. We will now concatenate every element across the given two lists

Input

inputList1 = ["hello", "tutorialspoint", "python"]
inputList2 = ["coding", "articles"]

Output

Resultant paired combinations list:
['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']

Method 1: Using the List Comprehension and String Concatenation

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input list 1 and print the given first list.

  • Create another variable to store the input list 2 and print the given second list.

  • Traverse in the first list and also in the second list(nested loops) using list comprehension.

  • Inside it create tuple pairs with the first element from the first list and the second element from the second list.

  • Concatenate the pair elements using string concatenation by iterating through the pairs list.

  • Print resultant paired combinations list

Example

The following program returns the concatenation of every element across lists using list comprehension and string concatenation –

# input list 1
inputList1 = ["hello", "tutorialspoint", "python"]

# input list 2
inputList2 = ["coding", "articles"]

# printing input list 1
print("Input List 1:", inputList1)

# printing input list 2
print("Input List 2:", inputList2)

# Using a list to traverse the first list and the second list

# Creating tuple pairs with the first element from the first list

# and the second element from the second list
pairs_list = [(p, q) for p in inputList1 for q in inputList2]

# Iterating in the above pairs list and

# concatenating the tuples(string concatenation)
resultantList = [m + ' ' + n for (m, n) in pairs_list]

# printing resultant paired combinations list
print("Resultant paired combinations list:\n", resultantList)

Output

On executing, the above program will generate the following output –

Input List 1: ['hello', 'tutorialspoint', 'python']
Input List 2: ['coding', 'articles']
Resultant paired combinations list:
['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']

Time Complexity − O(n^2) as there are two for loops

Space Complexity − O(n)

Method 2: Using itertools.product() Function

Itertools.product() returns the Cartesian Product.

Let us look at what is Cartesian Product is -

In terms of mathematics, the set of all ordered pairs (a, b) where a belongs to A and b belongs to B is referred to as the Cartesian Product of Two Sets. Take a look at the sample below for a better understanding.

Example

list 1= [10, 20, 30]
list2 = [2, 4, 6]

Output

[(10, 2), (10, 4), (10, 6), (20, 2), (20, 4), (20, 6), (30, 2), (30, 4), (30, 6)]

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword to import the product() function from itertools module.

  • Create a variable to store the input list 1 and print the given first list.

  • Create another variable to store the input list 2 and print the given second list.

  • Take an empty list to store the result.

  • Traverse in the cartesian product of the given lists using the product() function.

  • Concatenate the two list elements using the + operator(string concatenation).

  • Add this pair string to the result using the append() function.

  • Print the result.

Example

The following program returns the concatenation of every element across lists using itertools.product() and string concatenation –

# importing product from itertools module
from itertools import product

# input list 1
inputList1 = ["hello", "tutorialspoint", "python"]

# input list 2
inputList2 = ["coding", "articles"]

# printing input list 1
print("Input List 1:", inputList1)

# printing input list 2
print("Input List 2:", inputList2)

# Taking an empty list to store the concatenation result
resultantList = []

# Iterating the cartesian product of the two lists
for e in product(inputList1, inputList2):
   
   # Concatenating two lists elements with space as a separator
   pairString = e[0]+' '+e[1]

   # Adding this pair string to the result list at the end of the result list
   resultantList.append(pairString)

# printing resultant paired combinations list
print("Resultant paired combinations list:\n", resultantList)

Output

On executing, the above program will generate the following output –

Input List 1: ['hello', 'tutorialspoint', 'python']
Input List 2: ['coding', 'articles']
Resultant paired combinations list:
['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']

Conclusion

In this article, we learned how to concatenate every element from the two lists that were given using two different methods. Additionally, we learned what the cartesian product is, how to compute it using the product() method, and examples.

Updated on: 27-Jan-2023

434 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements