9

I'm trying to construct a one liner that would check if any of the values in one list are present in another list and return True or False if it does or does not.

The closest I've gotten to is the following:

[i in list1 for i in list2]

The problem with this is that it will iterate through list1 and output a list of True and Falses depending on if the items in list1 exist in list2.

What I can do is then iterate through this newly created True and False list but I can't do that in the same line. I can't use a set in this case or import any functions as I'm using this as a condition in a third party software where you can't insert sets in conditions or use functions.

2
  • What have you tried so far? Pls see stackoverflow.com/help/how-to-ask and stackoverflow.com/help/mcve
    – Jimbo
    Jul 14, 2017 at 8:57
  • I have tried [i in list1 for i in list2] and [i for i in list1 if i in list2] But both do not return what I need. I've also looked at different ways to do this with sets or not in 1 line, however I can't use those here. Jul 14, 2017 at 9:04

3 Answers 3

17

You can us an any(..) builtin function with a generator expression:

any(e in list2 for e in list1)

So this will check if there is at least one element that occurs in both lists.

Note however that this will result in a worst-case O(n2) algorithm. If the elements are hashable for instance, and you can use a set, we can make it an O(n) average-case algorithm.

3
  • that actually works flawlessly, just tested it. I'm aware that having this whole code in one line will cause performance issues but it's the only thing I can use unfortunately. Jul 14, 2017 at 9:15
  • This is about the best performance you can get if the elements aren't hashable. O(n^2) will always be your worst case, because no matter what order you iterate through your lists, there's always the possibility that the only element in common is the very last one of each list. any is smart enough to stop the iteration as soon as it finds a match.
    – perigon
    Jul 14, 2017 at 9:17
  • if elements are hashable then ONLY use set !!! ... Wonderful answer ... Jun 10, 2020 at 16:08
6

You could also do

set(list1).intersection(list2)

to get the set of elements that occur in both; the length of the set is 0 if there's no intersection, otherwise positive. You can treat this set as a boolean, since Python evaluates empty sets to False and nonempty sets to True.

if set(list1).intersection(list2):
    print ('Lists have elements in common')
else: 
    print ('No elements in common')

Runtime is O(n) average-case, O(n^2) worst-case: https://wiki.python.org/moin/TimeComplexity

3
  • I can't use sets unfortunately as it's not an accepted expression in the condition tag of the system we're scripting in, I can use it otherwise though. Jul 14, 2017 at 9:27
  • Don't 100% understand what you mean, but if the problem is with treating sets as booleans, you could also do if len(set(list1).intersection(list2)) > 0:. It's a little ugly, though.
    – perigon
    Jul 14, 2017 at 9:30
  • This condition is being implemented in a system that is based on XML. In that system there are XML conditions attributes (cond's) in which you insert actual python conditions that evaluate. Those conditions are written in python but there's a limit as to what you can include in those conditions and the limit in this case is that you can't go on 2 lines and you can't use sets in them.I hope that makes sense. Jul 14, 2017 at 9:48
3

Supose we have this lists:

list1 = ['bar', 'foo', 'qwerty', 9, 1]
list2 = [1, 2, 3, 'foo']

If we want to see the repeated values:

[i for i in list1 if i in list2]

The input:

['foo', 1]

If we want True false:

(map(lambda each: each in list1, list2))

The input:

[True, False, False, True]

The input is checking list2, the first value '1' exists in list1 and the last 'foo' too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.