# Python | Build list of Cartesian Cartesian system

|

Method # 1: Using List Comprehension
This is a one-line way to accomplish this particular task. In doing so, we simply shorten the looping task to one line to generate all possible pairs of tuples with list items.

 ` # Python3 code to demonstrate how it works ` ` # Build Cartesian Cartesian system list ` ` # using comprehension list `   ` # initialize list and tuple ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 6 ` `, ` ` 7 ` `] ` ` test_tup ` ` = ` ` (` ` 1 ` `, ` ` 3 ` `) ` ` `  ` # print original list and tuple ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) ` ` print ` ` (` ` "The original tuple:" ` ` + ` ` str ` ` (test_tup)) ` ` `  ` # Build a Cartesian Cartesian system list ` ` # using a comprehension list ` ` res ` ` = ` ` [(a, b) for a in test_tup for b in test_list] ``   # print result print ( "The Cartesian Product is:" + str (res)) `

Output:

The original list: [1, 4, 6, 7]
The original tuple: (1, 3)
The Cartesian Product is: [(1, 1), (1, 4), (1, 6), (1, 7), (3, 1 ), (3, 4), (3, 6), (3, 7)]

Method # 2: Using ` itertools.product () `
This task can also be done with a single fu A function that internally performs the task of returning the required Cartesian product.

 ` # Python3 code to demonstrate how it works ` ` # Build a list of Cartesian Cartesian system ` ` # using itertools.product () ` ` from ` ` itertools ` ` import ` ` product `   ` # initialize list and tuple ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 6 ` `, ` ` 7 ` `] ` ` test_tup ` ` = ` ` ( ` ` 1 ` `, ` ` 3 ` `) `   ` # print original list and tuple ` ` print ` ` (` ` "The original list:" ` ` + ` ` str ` ` (test_list)) ` ` print ` ` (` ` "The original tuple:" ` ` + ` ` str ` ` (test_tup)) `   ` # Build a list of the Cartesian Cartesian system ` ` # using itertools.produc t () ` ` res ` ` = ` ` list ` ` (product (test_tup, test_list)) `   ` # print result ` ` print ` ` (` `" The Cartesian Product is: "` ` + ` ` str ` ` (res )) `

Output:

The original list: [1, 4, 6, 7]
The original tuple: (1, 3)
The Cartesian Product is: [(1, 1), (1, 4), (1, 6), (1, 7), (3, 1), (3, 4), (3, 6), (3, 7)]

## Shop Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$ Best laptop for Zoom

\$499 Best laptop for Minecraft

\$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method