Home > Net >  adding many lists into a nested dictionary in for loop in python
adding many lists into a nested dictionary in for loop in python

Time:10-03

my homework is that I want to create five lists and append these five lists into a dictionary. for example:

student_names = ['Mike', 'Sarah', 'Matthew','Smith', 'Melanie', 'Renjith', 'Rahul', 'Mark', 
'Rebeka','Jenna']

students_math_marks = [91.99,94.43,89,85.23,90.31,88,78.6,92,94.35,80.72]

students_english_marks = [83.56,95.34,83,90.44,88.32,95,97.7,91,95.54,89.32]

students_physics_marks = [92.19,89.51,90,88.55,89.45,97,89.8,87,81.72,93.54]

students_chemistry_marks = [89.64,87.63,98,94.66,93.78,85,86.9,95,84.81,91.86]

question 1: get a dictionary.

Sample Dictionary structure should be as follows (example):

student_info = {'Mike': {'math': 91.99, 'english': 83.56, 'physics': 92.19, 'chemistry': 89.64, 
'total_marks': 357.38}}

question 2: the final output is as follow:

enter image description here

my code is as follow:

student_names = ['james','john','david','michael','richard','paul','mark','william','thomas','steven']
students_math_marks  = [90.11,89.23,99.99,93.33,92.11,85.31,93.90,93.88,92.82,89.09]
students_english_marks = [88.91,90.21,97.35,98.44,93.55,96.01,99.99,99.50,94.88,95.05]
students_physics_marks = [99.05,92.33,94.44,95.55,96.66,97.77,98.88,99.99,91.11,90.98]
students_chemistry_marks = [90.05,99,91.88,92.77,93.66,94.55,95.44,96.33,97.22,98.11,99.99] 

print('name     math        english        physics        chemistry      total marks ')

print('----------------------------------------------------------------------------------')

student_info = {} # professor ask us to create an empty dictionary.

for n,m,e,p,c in zip(student_names,students_math_marks,students_english_marks,students_physics_marks,students_chemistry_marks):
    sum=round((m e p c),2)
    student_info = {n:{"math":float(m),'english':float(e),'physics':float(p),'chemistry':float(c),'total_marks':sum}}
    for key, value in student_info.items():
        print(f"\n{key}", end = '     ')
        for inkey, invalue in value.items():
            print(invalue, end = '         ')

my output is as follow: enter image description here

CodePudding user response:

You can calculate the length of the word subtract it from the spaces you need between the words and then add it in the end.

student_names = ['james','john','david','michael','richard','paul','mark','william','thomas','steven']
students_math_marks  = [90.11,89.23,99.99,93.33,92.11,85.31,93.90,93.88,92.82,89.09]
students_english_marks = [88.91,90.21,97.35,98.44,93.55,96.01,99.99,99.50,94.88,95.05]
students_physics_marks = [99.05,92.33,94.44,95.55,96.66,97.77,98.88,99.99,91.11,90.98]
students_chemistry_marks = [90.05,99,91.88,92.77,93.66,94.55,95.44,96.33,97.22,98.11,99.99] 

print('name           math           english        physics       chemistry      total marks ')

print('----------------------------------------------------------------------------------')

student_info = {} # professor ask us to create an empty dictionary.
spacek =0
spacev =0
for n,m,e,p,c in zip(student_names,students_math_marks,students_english_marks,students_physics_marks,students_chemistry_marks):
    sum=round((m e p c),2)
    student_info = {n:{"math":float(m),'english':float(e),'physics':float(p),'chemistry':float(c),'total_marks':sum}}
    for key, value in student_info.items():
        spacek =15-len(key)
        print(f"\n{key}",end=spacek*" ")
        for inkey, invalue in value.items():
            spacev =15-len(str(invalue)) 
            print(invalue, end = spacev*" ")# your code goes here
  • Related