Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - checking if an element is in two lists at the same time

Tags:

python

list

In Python, to check if an element is in two lists, we do

if elem in list1 and elem in list2:

Can we do the following for this purpose?

if elem in (list1 and list2):
like image 282
Muhammad Waqar Avatar asked May 16 '13 20:05

Muhammad Waqar


People also ask

How do you check if two lists share the same element in Python?

Using traversal in two lists, we can check if there exists one common element at least in them. While traversing two lists if we find one element to be common in them, then we return true. After complete traversal and checking, if no elements are same, then we return false.

How do you compare values in two lists in Python?

The cmp() function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1.

How do you check if elements of one list are 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.


2 Answers

No, you cannot.

list1 and list2 means "list1 if it's empty, list2 otherwise". So, this will not check what you're trying to check.

Try it in the interactive interpreter and see.


The simple way to do this is the code you already have:

if elem in list1 and elem in list2:

It works, it's easy to read, and it's obvious to write. If there's an obvious way to do something, Python generally tries to avoid adding synonyms that don't add any benefit. ("TOOWTDI", or "There should be one-- and preferably only one --obvious way to do it.")


If you're looking for an answer that's better in some particular way, instead of just different, there are different options depending on what you want.

For example, if you're going to be doing this check often:

elems_in_both_lists = set(list1) & set(list2)

Now you can just do:

if elem in elems_in_both_lists:

This is simpler, and it's also faster.

like image 67
abarnert Avatar answered Oct 18 '22 20:10

abarnert


No, the statement

if elem in (list1 and list2):

would not work for this specified purpose. What the Python interpreter does is first check list1, if found empty (i.e - False), it just returns the empty list (Why? - False and anything will always result in a false, so, why check further? ) and if not empty (i.e evaluated to True), it returns list2 (Why? - If first value is True, the result of the expression depends on the second value, if it is False, the expression evaluates to False, else, True.) , so the above code becomes if elem in list1 or if elem in list2 depending on your implementation. This is known as short circuiting.

The Wiki page on Short Circuiting might be a helpful read.

Example -

>>> list1 = [1, 2]
>>> list2 = [3, 4]
>>> list1 and list2
[3, 4]

>>> list1 = []
>>> list2 = [3, 4]
>>> list1 and list2
[]
like image 30
Sukrit Kalra Avatar answered Oct 18 '22 21:10

Sukrit Kalra