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](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2023/02/Python-if-any-in-the-list-example.jpg?resize=293%2C137&ssl=1)
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](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2023/02/Fastest-way-to-check-if-a-value-exists-in-a-list.png?resize=843%2C509&ssl=1)
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.