How to check in Python if one of the elements of a list is in another list

As the title suggests, there may be occasions when you want to check if one of the elements in one list is in another. In my case, I needed it when solving the CheckiO problem, but I think I will have the opportunity to use it in practice.

When I searched for what happened, [This Stack Overflow article](http://stackoverflow.com/questions/10668282/one-liner-to-check-if-at-least-one-item-in-list -exists-in-another-list) I will introduce the method because it was organized.

Thing you want to do

The goal is to create a function that has one list a and another list b, and returns True if one of the elements in list a is in list b, False if it doesn't.

Example list that returns True

a = ['A', 'B']
b = ['B', 'C', 'D']

True because'B' in list a exists in list b

Example list that returns False

a = ['A', 'B']
b = ['C', 'D', 'E']

False because neither'A'and'B' in list a are in list b

Method

Method 1: Use any () and a generator expression

def func1(a, b):
    return any(i in b for i in a)

This is a method using any () and a generator expression. Use for i in a to extract the elements i one by one, and use ʻi in b` to check if the element i exists in b. Since it uses a generator, it returns True when it knows it exists.

Method 2: Use the intersection of set

def func2(a, b):
    len(set(a) & set(b)) != 0

This is a method of converting a and b into a set and checking if they have an intersection. It's pretty easy to read.

Method 3: Use isdisjoint ()

def func3(a, b):
    return not set(a).isdisjoint(b)

Use isdisjoint (), which is the opposite of method 2 and returns True if it has no intersection. Personally, I don't like not.

Summary

So far, I've summarized how to check if one of the elements in one list is in another.

I haven't compared the performance, but I think it's a good idea to choose the one that suits you while considering readability.

reference

python - one-liner to check if at least one item in list exists in another list? - Stack Overflow

Recommended Posts

How to check in Python if one of the elements of a list is in another list
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Check if the string is a number in python
How to pass the execution result of a shell command in a list in Python
How to get a list of files in the same directory with python
[python] How to check if the Key exists in the dictionary
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to identify the element with the smallest number of characters in a Python list?
I created a script to check if English is entered in the specified position of the JSON file in Python.
How to check if the contents of the dictionary are the same in Python by hash value
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[python] Check the elements of the list all, any
How to clear tuples in a list (Python)
Make a copy of the list in Python
How to remove duplicate elements in Python3 list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python
How to get the number of digits in Python
How to write a list / dictionary type of Python3
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
[Python] How to output the list values in order
How to mention a user group in slack notification, how to check the id of the user group
Create a python script to check if the link at the specified URL is valid 2
[Python] How to make a list of character strings character by character
How to shuffle a part of a Python list (at random.shuffle)
How to use the __call__ method in a Python class
[Python] How to write an if statement in one sentence.
How to use any or all to check if it is in a dictionary (Hash)
How to develop in a virtual environment of Python [Memo]
What to do if the progress bar is not displayed in tqdm of python
How to check if a value exists in an enum
How to count the number of occurrences of each element in the list in Python with weight
How to find the first element that matches your criteria in a Python list
How to connect the contents of a list into a string
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)
Get the value of a specific key up to the specified index in the dictionary list in Python
[Python] How to delete rows and columns in a table (list of drop method options)
How to quickly count the frequency of appearance of characters from a character string in Python?
How to know the internal structure of an object in Python
How to compare lists and retrieve common elements in a list
Try to get a list of breaking news threads in Python.
Delete a particular character in Python if it is the last
How to judge that the cross key is input in Python3
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to get the vertex coordinates of a feature in ArcPy
How to get a list excluding elements whose index is i ...?
Check if you can connect to a TCP port in Python
How to delete multiple specified positions (indexes) in a Python list
[Golang] Check if a specific character string is included in the character string
[Python] A program that rotates the contents of the list to the left
Check the behavior of destructor in Python
[Python] How to convert a 2D list to a 1D list
Display a list of alphabets in Python 3
Check if the URL exists in Python
How to get a stacktrace in python