Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering out if the dictionary list has the key in a set

events=[
    {'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:00:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'},  'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}]},
    {'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T08:00:00-0700', 'timeZone': 'America/Vancouver'}, 'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}]},
    {'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-17T00:00:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-17T00:30:00-0700', 'timeZone': 'America/Vancouver'}}
]
   

I would like to filter a dictionary list(slightly different list structure) if it has an [‘attendee’][’email’] that is valid(in emails) which is not the organizer. So [‘attendees’][‘organizer’] can’t be the one used.

emails=['b@hotmail.com'] 
for e in events:
    if e['attendees']['email'] in emails:
        print(e)

Produces

TypeError at /
list indices must be integers or slices, not str

Should Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 {'kind': 'calendar#event', 'etag': '"1"', 'id': '1', 'status': 'confirmed', 'htmlLink': 'https://www.google.com/calendar/event?eid=1', 'created': '2022-03-14T00:08:11.000Z', 'updated': '2022-03-14T00:08:12.162Z', 'summary': 'Appointment', 'description': 'Online appointment', 'location': 'Online', 'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:00:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'}, 'iCalUID': '1@google.com', 'sequence': 0, 'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}], 'reminders': {'useDefault': False, 'overrides': [{'method': 'email', 'minutes': 1440}, {'method': 'popup', 'minutes': 10}]}, 'eventType': 'default'},
 {'kind': 'calendar#event', 'etag': '"1"', 'id': '1', 'status': 'confirmed', 'htmlLink': 'https://www.google.com/calendar/event?eid=1', 'created': '2022-03-14T00:08:11.000Z', 'updated': '2022-03-14T00:08:12.162Z', 'summary': 'Appointment', 'description': 'Online appointment', 'location': 'Online', 'creator': {'email': 'a@hotmail.com', 'self': True}, 'organizer': {'email': 'a@hotmail.com', 'self': True}, 'start': {'dateTime': '2022-03-15T07:30:00-0700', 'timeZone': 'America/Vancouver'}, 'end': {'dateTime': '2022-03-15T08:00:00-0700', 'timeZone': 'America/Vancouver'}, 'iCalUID': '1@google.com', 'sequence': 0, 'attendees': [{'email': 'b@hotmail.com', 'responseStatus': 'needsAction'}, {'email': 'a@hotmail.com', 'organizer': True, 'self': True, 'responseStatus': 'needsAction'}], 'reminders': {'useDefault': False, 'overrides': [{'method': 'email', 'minutes': 1440}, {'method': 'popup', 'minutes': 10}]}, 'eventType': 'default'}

>Solution :

e[‘attendees’] is a list so you need to specify it’s index

for e in events:
    try:
        if e['attendees'][0]['email'] in emails:
            print(e)
    except KeyError:
        pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading