Home > OS >  how to print items of two lists in a same line in python. can't use any pre-defined functions e
how to print items of two lists in a same line in python. can't use any pre-defined functions e

Time:11-08

players_list = [a,b,c,d,e]
scores_list = [1,2,3,4,5]

I want the output to be like:

"-a_____________1-"

"-b_____________2-"

"-c_____________3_" 

and so on

I have tried using a nested loop, 2 loops and text formatting and lots of stuff but still can't figure out.

CodePudding user response:

players_list = ['a','b','c','d','e']
scores_list = [1,2,3,4,5]
for p, s in zip(players_list, scores_list):
    print(f"-{p}_____________{s}-")

CodePudding user response:

You can use zip function to combine multiple lists:

players_list = ['a','b','c','d','e']
scores_list = [1,2,3,4,5]

for player, score in zip(players_list, scores_list):
  print(f'-{player}_____________{score}-')

will print

-a_____________1-
-b_____________2-
-c_____________3-
-d_____________4-
-e_____________5-

Note that I added quotes around the characters in the players_list to make them strings and I used f-strings for formatting the output.

Alternatively, the print could be

print('-', player, '_____________', score, '-', sep='')

if you don't want to use f-strings.

  • Related