Home > Software design >  Cannot get the desired output in python
Cannot get the desired output in python

Time:09-21

I need to get this output in my python code: output

but I am getting index out of range error like this: error

here's my code:

marks = [['john',80, 90, 76, 82],['katy', 50, 55, 70, 65],['sydney',80,
72, 88, 90]]
marks_c = {}
for i in range(len(marks)):
    name = marks[i][0]
    l = []
    for j in range(2,len(marks[i])):
        print(marks[j][i])
        print(marks_c)

print(marks_c)

what am I doing wrong?

CodePudding user response:

Does this code deliver what you are looking for:

Code:

marks = [['john', 80, 90, 76, 82], ['katy', 50, 55, 70, 65], ['sydney', 80, 72, 88, 90]]
marks_c = {}

for entry in marks:
    marks_c[entry.pop(0)] = entry

print(marks_c)

# Upon request of the questioner:
#
# for i in range(len(marks)):
#     name = marks[i][0]
#     l = []
#     for j in range(1,len(marks[i])):
#         l.append(marks[i][j])
#     marks_c[name] = l

Output:

{'john': [80, 90, 76, 82], 'katy': [50, 55, 70, 65], 'sydney': [80, 72, 88, 90]}
  • Related