[Tutor] Count number of female names in dict

Manprit Singh manpritsinghece at gmail.com
Mon Jun 28 12:08:01 EDT 2021


Dear sir,
Consider a dict as given below :
dic = {"Miss Preeti":30, "Master Paras": 31, "Master Jay": 30, "Miss
Pooja": 32}

The keys are names of students and values are marks obtained by them . Now
if  i have to count the number of female students (Names starting with Miss
are female names ), can i do it in this way as given below ?

sum(x.startswith("Miss") for x in dic.keys())
See the generator expression inside sum() function will return an iterator
of  True & False (True for females ), So i can say that passing this
Generator expression inside sum() will give count of True that is 2 and is
correct answer (There are only 2 female names).
Is my way correct ?

Regards
Manprit Singh


More information about the Tutor mailing list