How to filter a dictionary ?

Wenhua Zhao whzhao at gmail.com
Wed Apr 11 14:21:48 EDT 2012


Hi,

Nikhil Verma writes:
 > In [9]: (k for k,v in for_patient_type.iteritems() if v == 'Real')

You can use dict comprehension, just change () to {}.

>>> for_patient_type = {37: u'Test', 79: u'Real', 80: u'Real', 81: u'Real', 83: u'Real', 84: u'Real', 91: u'Real', 93: u'Real'}
>>> {k:v for k,v in for_patient_type.iteritems() if v == 'Real'}                                                                                               
{79: u'Real', 80: u'Real', 81: u'Real', 83: u'Real', 84: u'Real', 91: u'Real', 93: u'Real'}                                                                   
>>> 

Thanks,
-- 



More information about the Python-list mailing list