Home > Back-end >  count grade inside sublist of dictionary which is inside a list
count grade inside sublist of dictionary which is inside a list

Time:07-27

i am fairly new to python, and i am learning how to loop through list, dictionaries tuples et... i made up this list with dictionaries and i want to know how to loop through the each dictionary and count the grades and add the sum of the grades together to know the total. i have added my loop after the dictionary.

    students = [
        {
            'first_name': 'Carlos',
            'last_name': 'Santiago',
            'class':'French',
            'grades': [
                'A',
                'B ',
                'C'
            ]
        },
        {
            'first_name': 'Dana',
            'last_name': 'Lynn',
            'class':'Spanish',
            'grades': [
                'D',
                'A ',
                'A',
                'B'
            ]
        },
        {
            'first_name': 'Fin',
            'last_name': 'leroy',
            'class':'Math',
            'grades': [
                'B',
                'B ',
                'A',
                'A ',
                'B'
            ]
        }
    ]```



    ```sum =0
       for i in students:
         if i =='grades':
           for v in students['grades']:
            sum =v
            print(sum)```

CodePudding user response:

  • use enumerate to loop through the elements of your list
  • make sure to reset the sum for every student inside the loop
for student in enumerate(students):
    sum = 0
    list_of_grades = student[1]['grades']
    for grade in list_of_grades:
        *Your code to add the grades goes here*


CodePudding user response:

To print the sum (concatenation in your case) of the grades of each student :

for student in students:
    sum = ''.join(student['grades'])
    print(sum)

Example output :

AB C

summing strings concatenates them.

If you change grades to be something like :

'grades': [ 5, 7, 10, 3]

Then :

for student in students:
    sum = sum(student['grades'])
    print(sum)

Outputs : 25

CodePudding user response:

First, you want to avoid using built-in function names (e.g., sum) as your object name.

Second, I assume you meant to get the sum of integers or floats. So I replaced your string grades with integers.

students_new = [
    {
        'first_name': 'Carlos',
        'last_name': 'Santiago',
        'class':'French',
        'grades': [
            1,
            2,
            3
        ]
    },
    {
        'first_name': 'Dana',
        'last_name': 'Lynn',
        'class':'Spanish',
        'grades': [
            1,
            2,
            3,
            4
        ]
    },
    {
        'first_name': 'Fin',
        'last_name': 'leroy',
        'class':'Math',
        'grades': [
            1,
            2,
            3,
            4,
            5
        ]
    }
]

total=0
for i in students_new:
    i_sum = sum(i['grades'])
    print(i['last_name'], i['class'], 'grade:', i_sum)
    total =i_sum
print(total)

CodePudding user response:

IIUC, this can simply be done in a one liner using list comprehension en value_counts:

pd.Series([g for l in [rec['grades'] for rec in students] for g in l]).value_counts()

Output:

A     3
B     3
B     2
A     2
C     1
D     1
dtype: int64
  • Related