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:
{'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