Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.

My code looked like this:

my_list = [x for x in my_list if x.attribute == value]

But then I thought, wouldn't it be better to write it like this?

my_list = filter(lambda x: x.attribute == value, my_list)

It's more readable, and if needed for performance the lambda could be taken out to gain something.

Question is: are there any caveats in using the second way? Any performance difference? Am I missing the Pythonic Way™ entirely and should do it in yet another way (such as using itemgetter instead of the lambda)?

1 Answer

0 votes
by (106k points)
edited by

Actually, list comprehension is much clearer and faster than filter+lambda, but you can use whichever you find easier. 

If you are using a filter then there are two things that slow down you:

The first thing is the function call overhead: as soon as you use a Python function (whether created by def or lambda) it is likely that the filter will be slower than the list comprehension.

The second thing that can affect you is, that the lambda is being forced to access a scoped variable (value). Which is slower than accessing a local variable and in Python 2.x. Whereas list comprehension can only access local variables. If you are using Python 3.x then the list comprehension runs in a separate function so it will also be accessing the value through the closure and this difference won't apply.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 29, 2019 in Python by Rajesh Malhotra (19.9k points)
0 votes
1 answer
asked Jul 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...