I am trying to understand how the filter() function works and would like to know how I could write:
filter()
test = filter(lambda x: x == y, lst)
using for or while loops.
for
while
filter() pretty much creates a new list with a for loop and a conditional. In your example, it is identical:
L = [] for i in lst: if i == y: L.append(i)
Or as a list comprehension:
[i for i in lst if i == y]
Adding Haidros to the answer, in Python 3 the filter is a generator, so you can implement it like this:
filter
def filter (test, lst): for x in lst: if test(x): yield x
Source: https://habr.com/ru/post/1492926/More articles:JQuery UI Sortable - Div Scrolling Problem - jqueryDisable button on click until actionPerformed is complete java - javaKeyboard Shortcut Event - wpfLog in to Google OAuth 2.0 on iOS - iosCan I fulfill the call counter statement at compile time? - c ++How to insert long text into a UITextView - ioshow to see styles applied when hovering an element in - cssAndroid ImageView centerCrop image type - javaPhonegap function not calling device - javascriptC #, JSON Parsing, dynamic variable. How to check type? - c #All Articles