Home > database >  List within a list formatting
List within a list formatting

Time:06-08

index = 0
player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15],['Jessica Jones', 12, 0, 6, 6, 10, 6],['Johnny Rose', 6, 2, 0, 4, 20, 10],['Gina Linetti', 7, 4, 0, 3, 300, 15],['Buster Bluth', 3, 0, 2, 1, 50, 1]]

while index < len(player_list):

    player_list2 = player_list[index]
    index  =1
    
    index_1 = 0 
    while index_1 < len(player_list2):
        print(player_list2[index_1])
        index_1  =1

I want the list to be printed in the following format:

Bruce Wayne 5 5 0 0 100 15 
Jessica Jones 12 0 6 6 10 6
Johnny Rose 6 2 0 4 20 10
Gina Linetti 7 4 0 3 300 15
Buster Bluth 3 0 2 1 50 1

I've tried sep and end but they do not seem to work.

CodePudding user response:

You could use string join here:

player_list = [['Bruce Wayne', 5, 5, 0, 0, 100, 15],['Jessica Jones', 12, 0, 6, 6, 10, 6],['Johnny Rose', 6, 2, 0, 4, 20, 10],['Gina Linetti', 7, 4, 0, 3, 300, 15],['Buster Bluth', 3, 0, 2, 1, 50, 1]]
for player in player_list:
    print ' '.join([str(x) for x in player])

This prints:

Bruce Wayne 5 5 0 0 100 15
Jessica Jones 12 0 6 6 10 6
Johnny Rose 6 2 0 4 20 10
Gina Linetti 7 4 0 3 300 15
Buster Bluth 3 0 2 1 50 1

CodePudding user response:

Try this

for x in player_list:
    # unpack each sub-list and print
    print(*x)

#Bruce Wayne 5 5 0 0 100 15
#Jessica Jones 12 0 6 6 10 6
#Johnny Rose 6 2 0 4 20 10
#Gina Linetti 7 4 0 3 300 15
#Buster Bluth 3 0 2 1 50 1
  • Related