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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Test if a list is completely True
- Python - Extract Row with any Boolean 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 - Test if all rows contain any common element with other Matrix
- 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 | Check if any element occurs n times in given list
- Python - Check if string starts with any element in list
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.