If I call filter() on a list, say:
>>> filter(lambda x: x > 1, [1,2,3,4])
[2, 3, 4]
Is this:
a) creating a totally new list in memory with the results
or
b) is it just pop()-ing items from the existing list and returning the same list?
Filter returns a new list containing elements of the expression that satisfy the given conditions. The original list is intact. In your case, it doesn't really matter, because your list is an in-place constant -- but I assume that you have a more general application in mind.
See the documentation: it states that filter constructs a new iterable (list, tuple, ...).
In Python2, filter() returns a new list object. The list passed into the function is unaffected. This can be observed using a very simple example:
>>> lst = [1, 2, 3, 4, 5]
>>> filter(lambda element: element < 4, lst)
[1, 2, 3]
>>> lst # lst is unaffected
[1, 2, 3, 4, 5]
>>>
In Python3, filter() also does not affect the list passed in. However, it does not return a new list object. Rather, it returns a filter object:
>>> lst = [1, 2, 3, 4, 5]
>>> filter(lambda element: element < 4, lst)
<filter object at 0x7fb69fef1b00>
>>> lst
[1, 2, 3, 4, 5]
>>>
If you want a list object, you need to explicitly cast the return value of filter() to a list:
>>> lst = [1, 2, 3, 4, 5]
>>> list(filter(lambda element: element < 4, lst))
[1, 2, 3]
>>>
In both cases though, your first hypothesis is correct. filter() does not do its work in place, it returns a new object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With