What is the difference between these two solutions - lambda or loop - Python

I want to calculate the sum of even numbers inside a domain. I have two solutions, but I am not sure about the advantages / disadvantages of each. What is the best solution?

import sys domain = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Cal1 = sum(filter(lambda n : n % 2 == 0, domain)) Cal2 = sum([n for n in domain if n % 2 == 0]) sys.stdout.write("Cal1 = {0}\n".format(Cal1)) sys.stdout.write("Cal2 = {0}\n".format(Cal2)) 
+7
python
source share
4 answers

The second really should be just a generator, not a list comprehension (since you really don't need to create a list in order to be able to sum the generator output):

 Cal2 = sum(n for n in domain if n % 2 == 0) 

This is now the preferred ("pythonic") way to accomplish this task.

  • Using list comprehension (including [] , your original Cal2 ) is disadvantageous since it actually creates a return list object that has overhead.

  • Using filter (your Cal1 ) is equivalent to a generator (version no- [] ), but it requires a bit more typing and doesn't read enough, it just uses a generator (the code I wrote above).

+12
source share

The following are the speeds of the various versions on a Mac Mac laptop:

 $ py26 -mtimeit -s'domain = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' 'sum(filter(lambda n : n % 2 == 0, domain))' 100000 loops, best of 3: 4.41 usec per loop $ py26 -mtimeit -s'domain = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' 'sum([n for n in domain if n % 2 == 0])' 100000 loops, best of 3: 2.69 usec per loop $ py26 -mtimeit -s'domain = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' 'sum(n for n in domain if n % 2 == 0)' 100000 loops, best of 3: 2.86 usec per loop 

Note that although the genexp version is undoubtedly steeper, the listcomp is slightly faster (perhaps not enough to worry about if this code is not in a tight inner loop that you are trying to optimize the snot). As usual, the lambda based version is significantly slower, as others have noted - lambda is a kind of "bad attitude" in Python :-(. (Not to mention that the def function is better here))

+6
source share

The second way to do this is through what is called list comprehension. List enumerations can be used to achieve the things you would previously use filter and map before introducing them into the language. See this previous question for a discussion of census lists and comparisons similar to what you are asking.

According to Amber , the recommended way for Pythonic to do this is to use a generator. With the comprehsions list, your entire filtered list is built and then added up. It sums up with the generator as it goes without having a complete list in memory. It matters more if you work with more than 10 elements.

+2
source share

+1 other great answers.

Bonus: Generator Expression Faster ...

 $ python -m timeit -s 'L = xrange(10)' 'sum(filter(lambda n: n % 2 == 0, L))' 100000 loops, best of 3: 3.59 usec per loop $ python -m timeit -s 'L = xrange(10)' 'sum(n for n in L if n % 2 == 0)' 100000 loops, best of 3: 2.82 usec per loop 

Docs re timeit .

+2
source share

All Articles