Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I want to calculate the sum of even numbers within a domain. I have two solutions, but I'm not sure of the advantages/disadvantages of each. Which is the optimal 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))
like image 769
kilobaik Avatar asked Jun 19 '10 18:06

kilobaik


2 Answers

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

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

It's the now-preferred ("pythonic") way for accomplishing this task.

  • Using a list comprehension (the one including the [], your original Cal2) is disadvantageous because it actually constructs a list object to return, which has overhead.

  • Using filter (your Cal1) is equivalent to a generator (the no-[] version), but requires a bit more typing and doesn't read quite as well as just using a generator expression (the code I posted above).

like image 151
Amber Avatar answered Oct 21 '22 03:10

Amber


Here are the speeds of the various versions on an old-ish 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, while the genexp version is no doubt more cool, the listcomp is marginally faster (probably not by enough to worry about unless this code is in a tight inner loop you're striving to optimize the snot out of;-). As usual, the lambda-based version is substantially slower, as others have mentioned -- lambda is kind of a "poor relation" in Python:-(. ((Not that a defined function would perform noticeably better here, either))

like image 36
Alex Martelli Avatar answered Oct 21 '22 02:10

Alex Martelli