Help counting the total number of dictionaries inside a list thatcontain a specified key value

J

Jon Bowlas

Hi All,

I have the following list containing dictionaries and I would like to
be able to count the total number of dictionaries I have that contain
a certain value set for the 'level' key:

[{'mod_title': u'Introduction to Human Anatomy', 'level': u'1',
'modcode': u'ANAT1003', 'deptleveltext': u'', 'deptlevelheader':
u'Level 1 Courses', 'subj_code': u'AE'}, {'mod_title': u'Developmental
Neurobiology', 'level': u'2', 'modcode': u'ANAT2008', 'deptleveltext':
u'', 'deptlevelheader': u'Level 2 Courses', 'subj_code': u'AE'},
{'mod_title': u'Human Neuroanatomy', 'level': u'2', 'modcode':
u'ANAT2010', 'deptleveltext': u'', 'deptlevelheader': u'Level 2
Courses', 'subj_code': u'AE'}, {'mod_title': u'Human Anatomy and
Embryology', 'level': u'2', 'modcode': u'ANAT2050', 'deptleveltext':
u'', 'deptlevelheader': u'Level 2 Courses', 'subj_code': u'AE'},
{'mod_title': u'Ethics of Fertility and Embryo Research', 'level':
u'2', 'modcode': u'ANAT2099', 'deptleveltext': u'', 'deptlevelheader':
u'Level 2 Courses', 'subj_code': u'AE'}, {'mod_title': u"Man's Place
in Nature 1750-1900", 'level': u'23', 'modcode': u'HMED3001',
'deptleveltext': u'', 'deptlevelheader': u'Level 2/3 Courses',
'subj_code': u'AE'}, {'mod_title': u'Medicine, Disease and Society,
Antiquity to Renaissance ', 'level': u'23', 'modcode': u'HMED3003',
'deptleveltext': u'', 'deptlevelheader': u'Level 2/3 Courses',
'subj_code': u'AE'}, {'mod_title': u'Madness and Society', 'level':
u'23', 'modcode': u'HMED3004', 'deptleveltext': u'',
'deptlevelheader': u'Level 2/3 Courses', 'subj_code': u'AE'}]

For example I'd like to kow how many dictionaries there are with a
level 1, 2 , 23 & 3 etc. How would one go about achieveing this?

Hope someone can help.

Cheers

Jon
 
P

Peter Otten

Jon said:
I have the following list containing dictionaries and I would like to
be able to count the total number of dictionaries I have that contain
a certain value set for the 'level' key:
For example I'd like to kow how many dictionaries there are with a
level 1, 2 , 23 & 3 etc. How would one go about achieveing this?

from collections import defaultdict
freq = defaultdict(int)
for course in courses:
freq[course[u"level"]] += 1
print freq

Peter
 
J

John Machin

Hi All,

I have the following list containing dictionaries and I would like to
be able to count the total number of dictionaries I have that contain
a certain value set for the 'level' key:

[snip]

For example I'd like to kow how many dictionaries there are with a
level 1, 2 , 23 & 3 etc. How would one go about achieveing this?

Assuming:
thelist = [etc etc etc]

q = set(['1']); print q, sum(d.get('level') in q for d in thelist)
q = set(['23']); print q, sum(d.get('level') in q for d in thelist)
q = set(['2', '23']); print q, sum(d.get('level') in q for d in
thelist)

produces:
set(['1']) 1
set(['23']) 3
set(['2', '23']) 7

Is that what you wanted? If you are sure that each dict will have a
'level' key, you can use d['level'] instead of d.get('level').

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top