![]() Using the Python filter() function to filter a list of tuples example ![]() Or you can use the list() function to convert the iterator to a list. Since the filter() function returns an iterator, you can use a for loop to iterate over it. The following shows how to use the filter() function to return a list of scores where each score is greater than or equal to 70: scores = įiltered = filter( lambda score: score >= 70, scores) In fact, you can pass any iterable to the second argument of the filter() function, not just a list. It returns an iterator for the elements where the fn() returns True. The filter() function iterates over the elements of the list and applies the fn() function to each element. The following shows the syntax of the filter() function: filter(fn, list) Code language: Python ( python ) Python has a built-in function called filter() that allows you to filter a list (or a tuple) in a more beautiful way. Third, show the filtered list to the screen. ![]() If the element is greater than or equal to 70, add it to the filtered list.
0 Comments
Leave a Reply. |