I'm taking a basic Python class and occasionally have one-off questions. Google/Stackoverflow searches usually help but not always. There's also an online tutoring service but they sometimes tell me they can't figure out the issue. I'm sure there are at least a few people here who know python. Here's where I'm stuck. This is just a snippet of the program, partially edited. All I want here is to print the Cartesian product of the two lists in this format: (A, 1) (A, 2) (A, 3) (B, 1) (B, 2) (B, 3) (C, 1) (C, 2) (C, 3) Spoiler Code: import itertools predef_listA = [A,B,C] predef_listB = [1,2,3] welcome = str(input('Type Yes')) if welcome == 'Yes': print('Here is the first list: ', predef_listA) print('Here is the second list: ', predef_listB) input('Press Enter.') from itertools import product for list1, list2 in product(predef_listA,predef_listB): print(list1, list2) Right now it's printing out like this: A 1 A 2 A 3 B 1 B 2 etc... (For my purposes I don't care if it prints out with parentheses, brackets, or neither.) I'm wondering if I should abandon itertools and do a for loop?
The easiest way is to use two for loops If you want the answer: Spoiler predef_listA = ['A','B','C'] predef_listB = [1,2,3] welcome = str(input('Type Yes')) if welcome == 'Yes': print('Here is the first list: ', predef_listA) print('Here is the second list: ', predef_listB) input('Press Enter.') from itertools import product for i in predef_listA: for j in predef_listB: print("({},{})".format(i, j),end="") print("\n")