Sometimes, we want to find an item in list with Python.
In this article, we’ll look at how to find an item in list with Python.
How to find an item in list with Python?
To find an item in list with Python, we can use the filter
function.
For instance, we write
matches = filter(fulfills_some_condition, lst)
to call filter
with the fulfills_some_condition
function and the lst
list.
The fulfills_some_condition
function takes the lst
item we check the filter condition for as a parameter and returns the condition of the item that matches the filter.
filter
returns an iterable that has the items that matches the filter condition.
Conclusion
To find an item in list with Python, we can use the filter
function.