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 | Count true booleans in a list
- Python | Get indices of True values in a binary list
- Python | Test if string contains element from list
- Python | Test list element similarity
- Python program to fetch the indices of true values in a Boolean list
- Python | Check if all elements in list follow a condition
- Python | True Record
- Python | First occurrence of True number
- Python | Replace elements in second list with index of same element in first list
- Python | Subtract two list elements if element in first list is greater
- Python | Segregate True and False value indices
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.