In Python, I have a list of dictionaries:
mylist = [ { 'name': 'James', 'school': 'UCLA', 'date_joined': 2001 },
{ 'name': 'Jack', 'school': 'UCLA', 'date_joined': 2001 },
{ 'name': 'Fisher', 'school': 'NYU', 'date_joined': 2003 }]
How can I check whether a certain dictionary matches existing record, based only on the name and school key/values?
So:
example1 = { 'name': 'James', 'school': 'UCLA', 'date_joined': 2007 }
example1 = { 'name': 'James', 'school': 'UCLA', 'date_joined': 2001 }
should both match, but
example3 = { 'name': 'James', 'school': 'MIT', date_joined': 2001 }
should not.
There’s obviously:
for m in myList:
if (m['name']==example['name'] and m['school']==example['school']):
match_found = True
continue
but is there a more compact way?
1 Answer