helooo.kittty
helooo.kittty

Reputation: 1

How to alternate output between two lists?

I am trying to output which card a player gets as the card is being handed out. My idea was to output each card by alternating from the two lists. I have included an example below.

Ex.
List1 = [Banana,Cherry,Orange,Apple,Tomato]
List2 = [Blue,Red,Orange,Yellow,Grey,Purple]

Output:

    Banana
    Blue
    Cherry
    Red
    Orange
    Yellow...

This is what I have so far.

for card in player_A:
    print("Player A is dealt= ",card,"")
for card in Player_B:
    print("Player B is dealt=",card,"")

How can I go about doing that?

Upvotes: 0

Views: 245

Answers (2)

DarrylG
DarrylG

Reputation: 17156

Use zip to alternate between the two lists.

for a, b in zip(List1, List2):
  print(f'Player A is dealt= {a}')
  print(f'Playber B is dealt= {b}')

Output

Player A is dealt= Banana
Playber B is dealt= Blue
Player A is dealt= Cherry
Playber B is dealt= Red
Player A is dealt= Orange
Playber B is dealt= Orange
Player A is dealt= Apple
Playber B is dealt= Yellow
Player A is dealt= Tomato
Playber B is dealt= Grey

Upvotes: 2

João Victor
João Victor

Reputation: 435

You can just do

for i in range(len(List1)):
    print("Player A is dealt= ", List1[i], "")
    print("Player B is dealt= ", List2[i], "")

Upvotes: -1

Related Questions