some_list = [{"email":"were@mail.com","id_array":1234},{"email":"repo@mail.com","id_array":9887}]
for example I want to know whether were@mail.com
email exists or not in the list.
some_list = [{"email":"were@mail.com","id_array":1234},{"email":"repo@mail.com","id_array":9887}]
for example I want to know whether were@mail.com
email exists or not in the list.
you could just use this:
any(item["email"] == "were@mail.com" for item in some_list)
# True
iterate over some_list
and check if any
of its items has the given email.
if you need to do this check often, you could speed it up by generating a set
that contains the mails only and then check with in
:
email_set = set(item["email"] for item in some_list)
"were@mail.com" in email_set
# True
Using list comprehension:
emails = [i['email'] for i in some_list]
[In]: 'were@mail.com' in emails
[Out]:
True
Also don't hesitate to check out efficiency of other methods if your list is big (after creating the list with only the mails): Fastest way to check if a value exist in a list
You can also use this:-
some_list = [{"email":"were@mail.com","id_array":1234},
{"email":"repo@mail.com","id_array":9887}]
email_list = []
for var in some_list:
email_list.append(var['email'])
if "were@mail.com" in email_list:
print("Yes you have required email.")
else:
print("No you do not have required email")
I hope it may help you.