-2
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.

Masoud
  • 1,270
  • 7
  • 12
Jyothsna Gadde
  • 175
  • 1
  • 7

3 Answers3

6

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
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

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

Alejandro
  • 64
  • 7
0

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.

Rahul charan
  • 765
  • 7
  • 15