Finding the Cartesian product of strings using Python


In this article, the user will learn how to find the cartesian product of a string using Python. String manipulation is possible using Python language. The wide varieties of inbuilt functions and operators are available in Python to perform certain operations on the string. The Python language is an eminent one and is primarily used in developing the applications for the specific need of the user. In this article, various methods used to find the Cartesian product of the given strings is found. The Cartesian product is used in Python language to get all possible combinations of the given string.

To Find Cartesian product of Strings using Python

The methods or approaches to find this involve three major approaches as listed below.

Approach

Approach 1 − Using the product() function

Approach 2 − Using nested for loop

Approach 3 − Using the lambda function

Approach 1: Python Program to find the Cartesian product of strings using the product() function

To find the Cartesian product of the strings, the itertools module is used as it can handle various complex functions.

Algorithm

  • Step 1 − The variables are defined with values as set1 and set2.

  • Step 2 − With the help of product() function, the two sets given are multiplied to form a new string of values.

  • Step 3 − The possibilities of the strings are mentioned in the variable named "cart_product" and visualized as lists.

  • Step 4 − When the below code runs, it returns the output.

Example

#importing the itertools module to use its function
import itertools

#values are assigned to the variables
set1 = ["welcome", "all", "here"]
set2 = ["see", "you", "soon"]
#product() function is used to get the cartesian product of two given sets
cart_product = list(itertools.product(set1,set2))

#the final output after the multiplication which returns all possibilities
output = [f"{a[0]}{a[1]}" for a in cart_product]
print(output)

Output

 ['welcomesee', 'welcomeyou', 'welcomesoon', 'allsee', 'allyou', 'allsoon', 'heresee', 'hereyou', 'heresoon']

Approach 2: Python Program to find the Cartesian product of strings using nested for loop

The nested for loop is used to iterate through the strings of the list using the split(), append(), and strip() methods.

Algorithm

  • Step 1 − The variables are defined with values as set1 and set2.

  • Step 2 − The empty variable is defined.

  • Step 3 − With the help of product() function, the two sets given are multiplied to form a new string of values.

  • Step 4 − With the help of split() function, the two sets given are divided to form a new string of values.

  • Step 5 − The possibilities of the strings are mentioned in the variable named "cart_product" and added as a list.

  • Step 6 − When the below code runs, it returns the output.

Example

#values are assigned to the variables
set1 = "welcome, all, here"
set2 = "see, you, soon"

#initializing the empty list
cart_product = []
#for loop is used to iterate through the strings of characters
#split() function is specified with a comma to separate the strings in set1
for a in set1.split(','):
   #split() function is specified with a comma to separate the strings in set2
   for b in set2.split(','):
      #append() is used to add all the combinations of the sets obtained
      cart_product.append(a.strip() + b.strip())

#the final output after the multiplication which returns all possibilities
print(cart_product)

Output

 ['welcomesee', 'welcomeyou', 'welcomesoon', 'allsee', 'allyou', 'allsoon', 'heresee', 'hereyou', 'heresoon']

Approach 3: Python Program to find the Cartesian product of strings using lambda function

The lambda functions are used along with the map() function and when the functions are not defined or it is unknown, the lambda function is used efficiently.

Algorithm

  • Step 1 − The variables are defined with values as set1 and set2.

  • Step 2 − The empty variable is defined.

  • Step 3 − With the help of the lambda method, the two sets given are multiplied to form a new string of values.

  • Step 4 − With the help of map() and filter() functions, the two sets given are divided to form a new string of values.

  • Step 5 − The possibilities of the strings are mentioned in the variable named "cart_product" and added as a list.

  • Step 6 − When the below code runs, it returns the output.

Example

#values are assigned to the variables
set1 = "welcome, all, here"
set2 = "see, you, soon"

#using the lambda function along with map() function
cart_product = list(map(lambda a: a[0].strip() + a[1].strip(), [(i,j) for i in set1.split(',') for j in set2.split(',')]))

#the final output after the multiplication which returns all possibilities
print(cart_product)

Output

 ['welcomesee', 'welcomeyou', 'welcomesoon', 'allsee', 'allyou', 'allsoon', 'heresee', 'hereyou', 'heresoon']

Conclusion

Python Programs are widely used to deal with large strings of values and to make it possible the library used is "itertools". With the help of this library, we can directly find the Cartesian product of a string element. When the above methods are known to the user, they can get varying methods to get solve the given problem.

Updated on: 25-Aug-2023

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements