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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course