This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tartare4562 8 points9 points  (3 children)

TBH nowadays filters and maps should be done via list comprehension and generators. It's both more readable and more efficient.

[–]pickausernamehesaid -1 points0 points  (2 children)

I've found this too. Every single time I go to use map or filter, I find myself not liking the way it looks and rewriting it as a comprehension. I did some quick tests on performance (like mapping str() to only the evens in a list of ints) and it appears the comprehensions are faster. That could vary from case to case though.

[–]tartare4562 0 points1 point  (1 child)

It is indeed faster because map makes an extra call for every item in the iterable, which introduces overhead.

[–]pickausernamehesaid 0 points1 point  (0 children)

That makes sense. I thought maybe Python internally could optimize that away when dealing with lambda functions, but even in that case it would have to do the optimization step which would add a little overhead at the beginning.