Python Gradebook From Horizontal To Vertical

Hiya codecademy fam!

I am currently on the Python Gradebook challenge (https://www.codecademy.com/paths/build-chatbots-with-python/tracks/python-3-data-structures-and-loops/modules/learn-python3-lists/projects/gradebook) and while I completed the challenge I wanted to do something extra and align the list in a vertical fashion.
Like So:
physics, 98
calculus, 97
poetry, 85
history, 88

I looked online and found a couple methods that have given me errors or not what I was expecting.

For reference I used this site for the methods used:

(For these methods I also had to utilize the syntax I learned in previous lessons on codecademy :wink: and not the ones asked for in the project )

Thank you all ahead of time :innocent:

Code For 1st Method:
input

gradebook = [['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]

#Computer Science Grade Came In!

gradebook.append(['computer science', 100])

#Visual Arts Grade Came In!

gradebook.append(['visual arts', 93])

#Full list of grades

print ("The original list is : " + str(gradebook)) 

for a, b, c, d, e, f in zip(*gradebook): 

  print(a, b, c, d, e, f)

Output:
physics calculus poetry history computer science visual arts
98 97 85 88 100 93

Code For 2nd Method:
Input:

gradebook = [['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]
#Computer Science Grade Came In!
gradebook.append(['computer science', 100])
#Visual Arts Grade Came In!
gradebook.append(['visual arts', 93])
#Full list of grades
print ("The original list is : " + str(gradebook)) 

for i in range(len(gradebook)): 
   for x in gradebook: 
       print(x[i], end =' ') 
   print() 

Output:
IndexError: list index out of range

Use the for loop to iterate through gradebook itself, there’s no need for indexing here.

for element in gradebook:
    print(element)

# Would lead to the following output-
['physics', 98]
['calculus', 97]
['poetry', 85]
['history', 88]
['computer science', 100]
['visual arts', 93]

You could get your exactly desired text by altering the arguments to print. Something like print(element[0], element[1]) would be about right (or unpack with *). If you wanted a different separator such as your ', ’ comma space then use the sep keyword- print(a, b, sep=', ') for example to change the separator when passing multiple arguments to print.

2 Likes

I think this is what you’re trying to achieve with your 2nd Method:

gradebook = [['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]

#Computer Science Grade Came In!
gradebook.append(['computer science', 100])

#Visual Arts Grade Came In!
gradebook.append(['visual arts', 93])

#Full list of grades
print ("The original list is : " + str(gradebook)) 

for i in gradebook:
    for j in i:
        print(j, end =' ')
    print()
 
# -------------Output-------------------------
"""
The original list is : [['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88], ['computer science', 100], ['visual arts', 93]]
physics 98 
calculus 97 
poetry 85 
history 88 
computer science 100 
visual arts 93 
"""

I hope this helps!

Happy Coding!

2 Likes

As @tgrtim pointed out, you can just loop through the gradebook without using range(len()).

If you want to format it a certain way, I think the easiest way is to use string interpolation:

for grade in gradebook:
  print(f'{grade[0]}: {grade[1]}')
2 Likes