473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
Aug 12 '08 #1
2 991
Jon Bowlas wrote:
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
Aug 12 '08 #2
On Aug 12, 9:09 pm, "Jon Bowlas" <m...@jonbowlas.comwrote:
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
Aug 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
9
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: karafire2003 | last post by:
Hey there i was wondering if someone can help me with my program. I thought i had it down...but i'm having some problems. Here's the assignment. Write a C function named change() that accepts a...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
0
by: Christian Heimes | last post by:
Jon Bowlas wrote: sum(u'Level 2 Courses' in dct for dct in yourlist) Christian
0
by: Jon Bowlas | last post by:
Many thanks for all your reponses, much appreciated. I'll get back to you on which is the best for me. BTW - yes John thats exactly what I wanted. Cheers Jon
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.