Sometimes, while coding in Python, we can have a problem in which we need to filter a list on basis of condition met by any of the element. This can have it’s application in web development domain. Let’s discuss a shorthand in which this task can be performed.
Method : Using
any() + list comprehension
The simplest way and shorthand to solve this problem is to combine the functionalities of inbuilt
any() and list comprehension for rendering condition logic and list iteration. The
any() returns true if any of the list element matches the condition.
The original list : [6, 4, 8, 9, 10] Does list contain any element divisible by 5? : True
- Python - Test if a list is completely True
- Python | Check if any element in list satisfies a condition
- Python | Get indices of True values in a binary list
- Python program to fetch the indices of true values in a Boolean list
- Python | Count true booleans in a list
- How to get element-wise true division of an array using Numpy?
- Python | First occurrence of True number
- Python | Segregate True and False value indices
- Python | True Record
- Python | Matrix True Summation
- Replace the column contains the values 'yes' and 'no' with True and False In Python-Pandas
- blank=True - Django Built-in Field Validation
- null=True - Django Built-in Field Validation
- unique=True - Django Built-in Field Validation
- Python | Check if any element occurs n times in given list
- Python - Check if string starts with any element in list
- Python - Check if any list element is present in Tuple
- Python | Check if all elements in list follow a condition
- Python program to find all the Combinations in the list with the given condition
- Python | Test list element similarity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.