Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3-x: Nested list in a for loop - how to get a "horizontal" output

for i, j in cards: 
# cards = a list containing list of cards - RANDOM OUTPUTS, more pairs can be added to the list depending on the number that the user puts in
        print(i)
        print(j)
        print("\t")

How do I make it so that the output becomes:

TS  6S  JS
AH  5S  AS

Instead of:

TS
AH

6S
5S

JS
AS

I posted a question similar to this but I wasn't being specific enough, and I edited too late. Apologies in advance

EDIT - 'cards' code:

deck = deck_create()
def deal_cards(deck, num_players):

    cards= []
    for i in range(num_players):

        hands.append([deck.pop(), deck.pop()])

    return hands
like image 325
DwightD Avatar asked Dec 31 '25 10:12

DwightD


1 Answers

You could get a little fancy and use zip(*) to transpose cards. Then printing is simple, like this:

cards=[("TS", "AH"), ("6S", "5S"), ("JS", "AS")]
for round in zip(*cards):
    print('\t'.join(round))

output:

TS  6S  JS
AH  5S  AS
like image 111
Matthias Fripp Avatar answered Jan 02 '26 23:01

Matthias Fripp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!