-2

I am trying to understand better the list comprehension:

I have the following code:

deck = [] 
for rank in ranks:
    for suit in suits:
        deck.append(('%s%s')%(rank, suit))

How can I transform in list comprehension? And is it more pythonic to have as list comprehension or not?

Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • [https://stackoverflow.com/questions/18072759/list-comprehension-on-a-nested-list][1] – Mtzw Mar 22 '18 at 11:29

1 Answers1

1
ranks = [1,2,3,4,5]
suits = [10,11,12,13,14,15]

deck = [] 
for rank in ranks:
    for suit in suits:
        deck.append(('%s%s')%(rank, suit))


deck_comp = [('%s%s')%(rank, suit) for rank in ranks for suit in suits]

print(deck == deck_comp)
zwep
  • 1,207
  • 12
  • 26