logo-MindMajix
PostPythonPython
T
Tim Parker

Posted on 20th July 2024|14 views

0
votes

Cartesian Product Python

How can I construct a cartesian product tuple list?

1 answers
Answers
P
rushi chowdary

Posted on 20th July 2024| views

You can do it by using list comprehension just like this

ex_list = [2, 4, 8]

ex_tup = (6, 1)

print("original list : " + str(ex_list))

print("original tuple : " + str(ex_tup))

res = [(x, y) for a in ex_tup for y in ex_list]

print("The Cartesian Product is : " + str(res)) 

Output:

Original list : [2, 4, 8]

Originat tuple : (6, 1)

The cartesian product is: [(6,2), (6,4), (6,8), (1,2), (1,4), (1,8)]

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo

  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos
Name
Course *
Email *
Phone Number