Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does filter() work in-place in python?

Tags:

python

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?

like image 666
LittleBobbyTables Avatar asked Jun 19 '26 04:06

LittleBobbyTables


2 Answers

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, ...).

like image 52
Prune Avatar answered Jun 20 '26 19:06

Prune


Python 2

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]
>>> 

Python 3

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.

like image 34
Christian Dean Avatar answered Jun 20 '26 17:06

Christian Dean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!