-1

I am a beginner with Python.

I have been struggling with creating a proper list comprehension for my deck of cards. I want to create four lists within a list where each "number index" has the fitting type. I'll try to show below.

This is what I want:

deck = [["1 Hearts, "2H", ..."13H"], ["1 Diamonds", "2D", ..."13D"], [....], [...]]

This is what I have:

value = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
deck_types = ["hearts", "diamonds", "clubs", "spades"]
deck = [[i] + value for i in deck_types]

Output:

I can't post images because my reputation is below 10, but I will describe it.

[["hearts", "1", "2", "3"....], ["Diamonds", "1", "2", "3"....], ["Clubs", "1"...], [....]]

As you can see, it's not exactly what I want. Each type becomes the nr [0] in every sublist. My plan is to combine the .pop() function with random.randint for card draw. So that when the first player draws a card, that card will be removed from the deck. Then the second player will not be able to draw the same card. When I then print the first players' card, I want to be able to see both type and number. Thus far I have only been able to see the number.

Like this:

card1 = deck[random.randint(1, 3)].pop(random.randint(2, 13))
card2 = deck[random.randint(1, 3)].pop(random.randint(2, 13))
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Kenso33
  • 51
  • 6
  • @PranavHosangadi, he needs a list of lists. – najeem Nov 05 '20 at 20:50
  • @najeem that's absolutely trivial to do. The accepted answer in the linked question is . `[i+str(j) for i in list1 for j in list2]`. To make it a list of lists, you would do `[[i+str(j) for i in list1] for j in list2]`. It's rare that you'll find the _exact_ answer to your precise requirements online. Often, finding something close enough should be able to point you in the right direction. – Pranav Hosangadi Nov 05 '20 at 20:57
  • :) if you think that's a trivial think for a python newbie to understand, then he wouldn't have asked this question at all! – najeem Nov 06 '20 at 07:50

3 Answers3

0

So, I don't think you can do this in one single list comprehension, because it requires 2 loops. Here is the deconstructed version.

values = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
deck = []
card_suits = ["hearts", "diamonds", "clubs", "spades"]
for suit in card_suits:
    suit_deck = []
    for number in values:
        card = f"{number} {suit}"
        suit_deck.append(card)
    deck.append(suit_deck)
    
print(deck)
[['1 hearts', '2 hearts', '3 hearts', '4 hearts', '5 hearts', '6 hearts', '7 hearts', '8 hearts', '9 hearts', '10 hearts', '11 hearts', '12 hearts', '13 hearts'], ['1 diamonds', '2 diamonds', '3 diamonds', '4 diamonds', '5 diamonds', '6 diamonds', '7 diamonds', '8 diamonds', '9 diamonds', '10 diamonds', '11 diamonds', '12 diamonds', '13 diamonds'], ['1 clubs', '2 clubs', '3 clubs', '4 clubs', '5 clubs', '6 clubs', '7 clubs', '8 clubs', '9 clubs', '10 clubs', '11 clubs', '12 clubs', '13 clubs'], ['1 spades', '2 spades', '3 spades', '4 spades', '5 spades', '6 spades', '7 spades', '8 spades', '9 spades', '10 spades', '11 spades', '12 spades', '13 spades']]

Or one using a list comprehension for each suit_deck

values = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
deck = []
card_suits = ["hearts", "diamonds", "clubs", "spades"]
for suit in card_suits:
    suit_deck = [f"{number} {suit}" for number in values]
    deck.append(suit_deck)
    
print(deck)
    
0

well, you just needed one more level of list.

deck = [[x+y for x in value] for y in deck_types]
najeem
  • 1,841
  • 13
  • 29
  • This seems to do the trick! I tried without the [] around the first part, and that gave me this: ["1 hearts", "1 diamonds", "1 clubs", "1 spades", "2 hearts", "2 diamonds", "2 clubs", "2 spades"....."13 hearts", "13 diamonds", "13 clubs", "13 spades"]. Just one big list with every card. I'm not really sure which solution is the best. – Kenso33 Nov 06 '20 at 11:18
  • well, I think that's something that you have to decide based on what you are doing with the rest of your code. Based on what you've written, I think a flat list will help you more than a nested list. That will help you re-order the whole deck as well. (to simulate shuffling). Drawing a card can be just just `deck.pop(0)`. – najeem Nov 06 '20 at 18:34
  • I had not realized this! Interesting. I did encounter a new problem after creating the deck though. I need to be able to see which player drew the highest card, and that becomes difficult with Strings. – Kenso33 Nov 07 '20 at 14:19
  • I'd recommend you take a look at creating classes. Each card can be an object derived from the `card` class and the `deck` can be another class. You can implement functions like `deck.shuffle`. For cards, you can check if a card is higher than another by implementing the `__gt__` or `__lt__` functions. – najeem Nov 07 '20 at 19:53
0
values = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
card_suits = ["hearts", "diamonds", "clubs", "spades"]
[[f"{number} {suit}" for number in values] for suit in card_suits]

[['1 hearts',
  '2 hearts',
  '3 hearts',
  '4 hearts',
  '5 hearts',
  '6 hearts',
  '7 hearts',
  '8 hearts',
  '9 hearts',
  '10 hearts',
  '11 hearts',
  '12 hearts',
  '13 hearts'],
 ['1 diamonds',
  '2 diamonds',
  '3 diamonds',
  '4 diamonds',
  '5 diamonds',
  '6 diamonds',
  '7 diamonds',
  '8 diamonds',
  '9 diamonds',
  '10 diamonds',
  '11 diamonds',
  '12 diamonds',
  '13 diamonds'],
 ['1 clubs',
  '2 clubs',
  '3 clubs',
  '4 clubs',
  '5 clubs',
  '6 clubs',
  '7 clubs',
  '8 clubs',
  '9 clubs',
  '10 clubs',
  '11 clubs',
  '12 clubs',
  '13 clubs'],
 ['1 spades',
  '2 spades',
  '3 spades',
  '4 spades',
  '5 spades',
  '6 spades',
  '7 spades',
  '8 spades',
  '9 spades',
  '10 spades',
  '11 spades',
  '12 spades',
  '13 spades']]
  • This works! Though I do not understand the syntax for your list comprehension. Specifically this part: f"{number} {suit}" – Kenso33 Nov 06 '20 at 11:19
  • That is called an F-string in python. It's a way to format a string with variables in-line. so, `f""` is a string with the formatting parameter ahead of it. When you add variable names inside of curly brackets, it prints that info into the string. So `f"{number} {suit}"` creates a string based on what `number` and `suit` equal in that part of the for loop. – Lauren Boland Nov 06 '20 at 15:39