An obvious case of a XY problem...
Your real problem is a wrong data structure that stores heterogenous informations (the student's name and it's marks) the same way. The RightSolution(tm) is to use a better data structure, ie:
student1 = {
"personal_infos" : {
"name": "Lloyd",
},
"marks": {
"homework": [90, 97, 75, 92],
"quiz": [88, 40, 94],
"test": [75, 90]
},
"totals": {}
"averages": {}
}
}
Once you have this, you don't have to test whether you have a string or num as value:
def eachSubjAverage(student):
for subject, marks in student["marks"].items():
total = sum(marks) #totalling each marks
student["totals"][subject] = total
average = total / (len(marks))
student["averages"][subject] = average
Note that you could layout your data differently, ie per subject:
student1 = {
"personal_infos" : {
"name": "Lloyd",
},
"subjects": {
"homework": {
"marks" : [90, 97, 75, 92],
"total" : None,
"average" : None
},
"quiz": {
"marks" : [88, 40, 94],
"total" : None,
"average" : None
},
"test": {
"marks" : [75, 90],
"total" : None,
"average" : None
},
},
}
def eachSubjAverage(student):
for subject, data in student["subjects"].items():
total = sum(data["marks"]) #totalling each marks
data["total"] = total
average = total / (len(data["marks"]))
data["average"] = average
Note that if you don't have the option to fix the data structure (external data or else), you still don't want to rely on type-checking (which is brittle at best) - you want to test the key itself, either by whitelisting the subjects names or blacklisting the "non-subjects" names, ie:
# blacklist non-subjects
NON_SUBJECTS = ("name",)
def your_func(student):
for key, value in student.items():
if key in NON_SUBJECTS:
continue
compute_stuff_here()
Oh and yes: adding the total and average in the marks list is also a good way to shoot yourself in the foot - once it's done, you can't tell wether the last two "marks" are marks or (total, average).