Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Iterating over a list of lists and printing the contents out

I have a list of lists that contains different characters and values that are assigned to them. So my list is something like:

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]]

I want to be able to make a loop that goes through each player and prints their name and all of their stats. I have tried to make many loops but they either print the stats from different players or it doesn’t print the whole list. The latest one that I have tried prints the first name and then the first 4 numbers and then stops.

index = 0
index1 = 0
while index < len(player_list):
    print(player_list[index1][index])
    index = index + 1
index1 += 1

The output to this code prints:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Bruce Wayne
5
5
0
0

And that’s it. I need it to go though the whole thing and print them out like this:

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

Edit: I need to be able to format the characters and their scores to this template:

============================================================
-                      Player Summary                      -
============================================================
-                              P  W  L  D   Chips   Score  -

>Solution :

If you need to align the columns, then use f-strings with a width specifier:

for name, *stats in player_list:
    print(f"{name:15}", *(f"{stat:4}" for stat in stats))

Output:

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

After edit, using template

For getting the specific layout you added to your question, do this:

print("""============================================================
-                      Player Summary                      -
============================================================
-                              P  W  L  D   Chips   Score  -""")

for name, p, w, l, d, chips, score in player_list:
    print(f"- {name:27} {p:2} {w:2} {l:2} {d:2} {chips:7} {score:7}  -")
print("============================================================")

Output:

============================================================
-                      Player Summary                      -
============================================================
-                              P  W  L  D   Chips   Score  -
- 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  -
============================================================
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading