Gradebook : is there an easier way to do #1-3?

Hi everyone, I was wondering if there was an easier way to write this code?

(Line 8) thanks.

‘Without using any methods’ means we cannot use zip(). We can use a loop, though.

gradebook = []
for i in range(len(subjects)):
    gradebook.append([subject[i], grades[i]])

Now if we consider .append() is also a method we could try,

    gradebook[i] = [subject[i], grades[i]]

Going to test to be sure this works.

Traceback (most recent call last):
  File "<pyshell#86>", line 2, in <module>
    gradebook[i] = [subjects[i], grades[i]]
IndexError: list assignment index out of range

So the only way we can do this without a method is to set up the array with placeholders:

>>> gradebook = [0, 0, 0, 0]
>>> for i in range(len(subjects)):
...     gradebook[i] = [subjects[i], grades[i]]
... 
...     
>>> gradebook
[['Physics', 98], ['Calculus', 97], ['Poetry', 85], ['History', 88]]
>>> 
3 Likes

Thank you so much for the explanation, this was exactly what I was looking for! Take care.

2 Likes