4

I have two lists, for example:

a = ["mail1", "mail2", "mail3", "mail4"]
b = ["mail2", "mail5"]

and I want to check if any of the elements in list b also appears in list a.

I wanted to know if there is a way (and what is it) to do this without a for loop.

Also I wanted to know how can I create a list of boolean values, where each value will be the result of the comparison of values a[i] and b[i], something like:

[z for i, j in zip(a, b)  z = i == j] # (just with the right syntax)

z will be 1 if in some place i == j, so I can check the array for any 'True' values.

timgeb
  • 76,762
  • 20
  • 123
  • 145
Yarden
  • 496
  • 1
  • 5
  • 16
  • Please mention what have you tried with python? Some code will be convenient! – ρss Jun 17 '14 at 18:18
  • Please keep it to *one* question per post. Your first is answered by using [sets](https://docs.python.org/2/library/stdtypes.html#set). Your second is almost there: `[i == j for i, j in zip(a, b)]`, but that'll be limited to the shortest length of the two lists. E.g. it'll return `[False, False]`. – Martijn Pieters Jun 17 '14 at 18:19

3 Answers3

12

You can use any:

any(x in a for x in b)

The nice thing about this generator expression is that any will return True as soon as the generator yields a True, i.e. there won't be redundant x in a lookups.

Edit:

You can improve the time complexity by making a set from a.

a_set = set(a)
any(x in a_set for x in b)

Regarding your new question:

[x == y for x,y in zip(a,b)]
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • What is the actual syntax of the any function? I tried to use it as `return any(x in a for x in b);` but it reports back from missiong ')', identifiers and semicolon. – Bobys Apr 03 '17 at 11:26
  • 1
    @Bobys `any` takes an iterable and checks whether any element of that iterable is truthy (i.e. `bool(element) == True`). Your statement is syntactically correct (the iterable is a generator expression) so the error you are getting must come from somewhere else. – timgeb Apr 03 '17 at 12:18
9

Elegant way is to use sets:

a = ["mail1", "mail2", "mail3", "mail4"]
b = ["mail2", "mail5"]

anb = set(a) & set(b)

print anb

if anb:
    print True

>>> set(['mail2'])
>>> True
dt0xff
  • 1,553
  • 1
  • 10
  • 18
  • 1
    definitely takes longer to build a two sets from non-trivial lists (in most cases) than it does to run a genexp in `any` – Adam Smith Jun 17 '14 at 18:23
  • 1
    Of course it does, it is just a smart way to get difference and to check it. Just wanted to show it, because too many people don't know how awesome sets are. – dt0xff Jun 17 '14 at 18:26
0

The any function takes in an iterable (See documentation here), so the answer should be any([x in a for x in b])

yennycheung
  • 551
  • 4
  • 6