Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a value in one list is in another list with a one-liner for an if statement in Python if I'm not using sets?

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.

like image 652
Boyan Anakiev Avatar asked Jul 14 '17 08:07

Boyan Anakiev


People also ask

How do you check if an element in one list is present in another?

Method #1: Using all() function all() is used to check all the elements of a container in just one line. Checks for all the elements of one list for existence in other lists.

How do you check if a string in a list is in another list Python?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list.

How do you find elements that are in one list but not another?

To find the elements in one list that are not in the other: Use a list comprehension to iterate over the list. Check if each element is not contained in the other list and return the result. The new list will only contain items from the first list that are not in the second list.


3 Answers

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.

like image 179
Willem Van Onsem Avatar answered Oct 18 '22 15:10

Willem Van Onsem


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

like image 43
perigon Avatar answered Oct 18 '22 15:10

perigon


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.

like image 36
Víctor López Avatar answered Oct 18 '22 15:10

Víctor López