There are several ways to approach this. I am gonna offer a couple of ideas. The first idea is probably most closely aligned with what you are describing/showing in your own code.
Approach 1: most similar to your code:
decks = {}
i = 0
for deck in range(6):
for suit in ['clubs', 'diamond', 'hearts', 'spades']:
for card in range(1, 14):
i += 1
print(f"card_{i} = tuple(color = {suit}, num = {card})")
card_1 = tuple(color = clubs, num = 1)
card_2 = tuple(color = clubs, num = 2)
...
card_12 = tuple(color = clubs, num = 12)
card_13 = tuple(color = clubs, num = 13)
card_14 = tuple(color = diamond, num = 1)
card_15 = tuple(color = diamond, num = 2)
Approach 2: uses itertools.product() to remove a for loop:
A potentially better approach that removes one layer of the nested for
loops is to use the itertools.product()
function.
from itertools import product
i = 0
for deck in range(6):
for suit, num in product(['clubs', 'diamond', 'hearts', 'spades'], range(1,14)):
i += 1
print(f"card_{i} = tuple(color = {suit}, num = {num})")
Approach 3: Better storage of the results
Having said all of that, it is not clear to me whether you want real tuple
s stored in memory OR if you simply want to print something that looks like a tuple to the screen. Storing the data in a dictionary
datatype would allow you to use that data later, if need be. Presuming that you want real tuple
s, the following code will store all the results of your work in some form of datatype, like a dictionary
of tuple
s.
NOTE: tuple
s don't really store date using named arguments as you have shown above, they are more like lists where elements are simply stored in them, so below in the code, you will see us storing the element without any sort of named arguments:
tuple(suit, num)
instead of tuple(color=suit, num=num)
Let's see how this works out.
from itertools import product
i = 0
decks = {}
for deck in range(6):
for suit, num in product(['clubs', 'diamond', 'hearts', 'spades'], range(1,14)):
i += 1
# NOTE: here we are storing the data in a dictionary
# NOTE: tuples don't store items using named values
decks[f"card_{i}"] = tuple((suit, num))
# NOTE: here we use the items() method on our decks dictionary
# to extract each key and value in pairs so we can print them.
for card_num, card in decks.items():
print(card_num, '=', card)
card_1 = ('clubs', 1)
card_2 = ('clubs', 2)
...
card_12 = ('clubs', 12)
card_13 = ('clubs', 13)
card_14 = ('diamond', 1)
card_15 = ('diamond', 2)