CRABOLO
CRABOLO

Reputation: 8793

How to write this Python code shorter?

I'm making a modified blackjack game. Only playing with the cards 2 through 9. No tens, face cards, or aces in this deck.

To start out I created the variable deck, which takes an empty list.
I then created 4 for loops to append the list of cards to the deck.

I would like to know if there's a shorter way to write the following:

import random

deck = []
for list_of_cards in range(2, 10):
    spades = 'spades'
    list_of_spades = str(list_of_cards) + ' of' + ' ' + spades
    deck.append(list_of_spades)
for list_of_cards in range(2, 10):
    clubs = 'clubs'
    list_of_clubs = str(list_of_cards) + ' of' + ' ' + clubs
    deck.append(list_of_clubs)
for list_of_cards in range(2, 10):
    diamonds = 'diamonds'
    list_of_diamonds = str(list_of_cards) + ' of' + ' ' + diamonds
    deck.append(list_of_diamonds)
for list_of_cards in range(2, 10):
    hearts = 'hearts'
    list_of_hearts = str(list_of_cards) + ' of' + ' ' + hearts
    deck.append(list_of_hearts)

and the result is like I want, which is:

['2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades', '8 of spades', '9 of spades', '2 of clubs', '3 of clubs', '4 of clubs', '5 of clubs', '6 of clubs', '7 of clubs', '8 of clubs', '9 of clubs', '2 of diamonds', '3 of diamonds', '4 of diamonds', '5 of diamonds', '6 of diamonds', '7 of diamonds', '8 of diamonds', '9 of diamonds', '2 of hearts', '3 of hearts', '4 of hearts', '5 of hearts', '6 of hearts', '7 of hearts', '8 of hearts', '9 of hearts']

Since I'm new to Python, I'm assuming that there is a way to write this shorter. Thanks for the help!

Upvotes: 1

Views: 144

Answers (5)

Simeon Visser
Simeon Visser

Reputation: 122496

Two for loops are enough:

deck = []
for suit in ('spades', 'hearts', 'diamonds', 'clubs'):
    for n in range(2, 10):
        deck.append('%i of %s' % (n, suit))

You can also write it using a list comprehension:

deck = ['%i of %s' % (n, suit) for suit in ('spades', 'hearts', 'diamonds', 'clubs') for n in range(2, 10)]

Upvotes: 1

tckmn
tckmn

Reputation: 59333

Why do you need 4 loops? And why do you need to create those useless strings? (+ ' of' + ' ' + spades is the same as ' of spades'...) (I fixed some other things too)

deck = []
for n in range(2, 10):
    deck.append('%i of spades' % n)
    deck.append('%i of hearts' % n)
    deck.append('%i of diamonds' % n)
    deck.append('%i of clubs' % n)

Or, use a nested loop:

deck = []
for n in range(2, 10):
    for s in ['spades', 'hearts', 'diamonds', 'clubs']:
        deck.append('%i of %s' % (n, s))

Upvotes: 5

DSM
DSM

Reputation: 353459

I'd use a list comprehension:

>>> suits = ["hearts","diamonds","clubs","spades"]
>>> deck = ["{} of {}".format(num, suit) for suit in suits for num in range(2,10)]
>>> len(deck)
32
>>> deck
['2 of hearts', '3 of hearts', '4 of hearts', '5 of hearts', '6 of hearts', '7 of hearts', '8 of hearts', '9 of hearts', '2 of diamonds', '3 of diamonds', '4 of diamonds', '5 of diamonds', '6 of diamonds', '7 of diamonds', '8 of diamonds', '9 of diamonds', '2 of clubs', '3 of clubs', '4 of clubs', '5 of clubs', '6 of clubs', '7 of clubs', '8 of clubs', '9 of clubs', '2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades', '8 of spades', '9 of spades']

The list comp here is equivalent to

deck = []
for suit in suits:
    for num in range(2, 10):
        deck.append("{} of {}".format(num, suit))

Upvotes: 2

leewz
leewz

Reputation: 3346

You don't need to use separate strings for "of" and " ", for example. You don't need a separate string to hold the word "diamonds". Just write " of diamonds".

That said, here's a solution using string formatting, list comprehension, and split instead of manually making an array of suits.

deck = ['%d of %s' % (face, suit) for suit in 'spades hearts clubs diamonds'.split() for face in range(2,10)]

Upvotes: 4

RickyA
RickyA

Reputation: 16029

deck = []
colors = ["spades", "clubs", "diamonds", "hearts"]
for color in colors:
    for list_of_cards in range(2, 10):
        deck.append("{0} of {1}".format(list_of_cards, color))

Upvotes: 1

Related Questions