Python | Test if any list element returns true for condition
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