[Tutor] creating new dictionary based on membership testing

ski norman at khine.net
Mon Mar 9 16:28:41 CET 2009


hello,

i have this list which contains a number of dictionaries.

 >>>d1 = [{'is_selected': False, 'id': 'AAC', 'title': 'Association of 
Airline Cons.'}, {'is_selected': False, 'id': 'AALA', 'title': 'Adv. 
Activity Licence. Auth.'}, {'is_selected': False, 'id': 'ABPCO', 
'title': 'Association of British Prof. Conf. Organisation'}, 
{'is_selected': True, 'id': 'ABTA', 'title': 'Association of British 
Travel Agents'}, {'is_selected': False, 'id': 'ABTOT', 'title': 
'Association of Bonded Travel Organisation Trust'}, {'is_selected': 
False, 'id': 'AERA', 'title': 'Association of Europe Rail Agents'}]

what would be the correct way to create a new list with the dictionaries 
but only for dictionaries with key

'is_selected': False

here is what I have tried, so far, perhaps there is a better and less 
error prone method:

 >>> d2 = []
 >>> for x in d1:
...     if False in x.values():
...             d2.append(x)
...
 >>> d2
[{'is_selected': False, 'id': 'AAC', 'title': 'Association of Airline 
Cons.'}, {'is_selected': False, 'id': 'AALA', 'title': 'Adv. Activity 
Licence. Auth.'}, {'is_selected': False, 'id': 'ABPCO', 'title': 
'Association of British Prof. Conf. Organisation'}, {'is_selected': 
False, 'id': 'ABTOT', 'title': 'Association of Bonded Travel 
Organisation Trust'}, {'is_selected': False, 'id': 'AERA', 'title': 
'Association of Europe Rail Agents'}]

Thank you
Norman







More information about the Tutor mailing list