Skip to content

Python if any in list

  • by

Python if any in list is checks for any element satisfying a condition and returns a True in case it finds any one element.

Python if any in the list example

Simple example code Check if any element in list satisfies a condition using any() function.

aList = [4, 2, 3, 1, 5, 6]

res = any(ele > 10 for ele in aList)

print(res)

Output:

Python if any in the list example

Fastest way to check if a value exists in a list

If you only want to check the existence of one element in a list use in operator.

a = [4, 2, 3, 1, 5, 6]

print(7 in a)

Output: False

is the fastest solution. Note though that

7 in set_data
Fastest way to check if a value exists in a list

Source: https://stackoverflow.com/questions/7571635/

Do comment if you have any doubts or suggestions on this Python any() function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *