Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:09:27+00:00 2026-06-17T06:09:27+00:00

While comparing various equivalent forms of filter(xs, lambda x: x != el) in Python,

  • 0

While comparing various equivalent forms of filter(xs, lambda x: x != el) in Python, I stumbled upon something that surprised me. Consider the following forms:

def method1(xs, el):
    p = lambda x: x != el
    return [x for x in xs if p(x)]

def method2(xs, el):
    return [x for x in xs if (lambda y: y != el)(x)]

I would expect that Python’d build the lambda only once, and then store it in a temporary variable, so that both forms perform about as well. Maybe even that method1 would perform worse due to the name lookup.

But when I benchmarked them, it turned out that method2 performed consistently worse than method1. Why is this? Is it rebuilding the lambda for every iteration?


My benchmark script (in a separate module, and expects methods to contain method1 and method2) is as follows:

import math, timeit

def bench(n,rho,z):
    pre = """\
import random
from methods import %(method)s

x = [(random.randint(0,%(domain)i)) for r in xrange(%(size)i)]
el = x[0]\
"""

    def testMethod(m):
        mod = pre % { 'method': m, 'domain': int(math.ceil(n / rho)), 'size': n }
        return timeit.timeit("%s(x, el)" % m, mod, number = z)/(z * n)

    print "Testing", n, rho, z
    return tuple(testMethod(m) for m in ("method1", "method2"))

n = 31

min_size, max_size = 10.0**1, 10.0**4
size_base = math.pow(max_size / min_size, 1.0/(n-1))
# size_default = 10**3

#min_sel, max_sel = 0.001, 1.0
#sel_base = math.pow(max_sel / min_sel, 1.0/(n-1))
sel_default = 0.001

tests = [bench(int(min_size*size_base**x), sel_default, 100) for x in xrange(n)]
#tests = [bench(size_default, min_sel*sel_base**x, 100) for x in xrange(n)]

def median(x):
    x = list(sorted(x))
    mi = int(len(x)/2)
    if n % 2 == 0:
        return x[mi]
    else:
        return (x[mi] + x[mi+1])/2

def madAndMedian(x):
    meh = median(x)
    return meh, median([abs(xx - meh) for xx in x])

for z in zip(*tests):
    print madAndMedian(z)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:09:28+00:00Added an answer on June 17, 2026 at 6:09 am

    Yes, it is rebuilding the lambda on every loop; it needs to reevaluate that whole expression.

    To see this, use the dis module:

    >>> dis.dis(method1)
      2           0 LOAD_CLOSURE             0 (el)
                  3 BUILD_TUPLE              1
                  6 LOAD_CONST               1 (<code object <lambda> at 0x102000230, file "<stdin>", line 2>)
                  9 MAKE_CLOSURE             0
                 12 STORE_FAST               2 (p)
    
      3          15 BUILD_LIST               0
                 18 LOAD_FAST                0 (xs)
                 21 GET_ITER            
            >>   22 FOR_ITER                24 (to 49)
                 25 STORE_FAST               3 (x)
                 28 LOAD_FAST                2 (p)
                 31 LOAD_FAST                3 (x)
                 34 CALL_FUNCTION            1
                 37 POP_JUMP_IF_FALSE       22
                 40 LOAD_FAST                3 (x)
                 43 LIST_APPEND              2
                 46 JUMP_ABSOLUTE           22
            >>   49 RETURN_VALUE        
    >>> dis.dis(method2)
      2           0 BUILD_LIST               0
                  3 LOAD_FAST                0 (xs)
                  6 GET_ITER            
            >>    7 FOR_ITER                33 (to 43)
                 10 STORE_FAST               2 (x)
                 13 LOAD_CLOSURE             0 (el)
                 16 BUILD_TUPLE              1
                 19 LOAD_CONST               1 (<code object <lambda> at 0x101fd37b0, file "<stdin>", line 2>)
                 22 MAKE_CLOSURE             0
                 25 LOAD_FAST                2 (x)
                 28 CALL_FUNCTION            1
                 31 POP_JUMP_IF_FALSE        7
                 34 LOAD_FAST                2 (x)
                 37 LIST_APPEND              2
                 40 JUMP_ABSOLUTE            7
            >>   43 RETURN_VALUE        
    

    The LOAD_CONST opcode loads the compiled code for the lambda body; MAKE_CLOSURE creates the lambda from that. For method1 this happens once, while for method2 this is repeated in each iteration of the loop (from the FOR_ITER opcode to the JUMP_ABSOLUTE opcode); note the LOAD_FAST opcode for the variable p in method1 where it refers to the local variable instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem while comparing the values in two vectors. Following is the
I'm using LINQ to NHibernate and encountered a strange problem while comparing strings. Following
While Compiling the WPF Application for many times in a day it give following
I am getting the following error while compiling a sample program in Qt 4.5
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class
Suppose I have a Java Servlet that takes a while to finish computing it's
I encountered a strange and unexpected behavior in PHP while comparing some string values.
I was comparing a simple hash function that I wrote which just multiplies it
i am creating various nsmanagedobjects from nsdictionaries and while i am iterating over the
I've been having some 'strange' results while comparing dates. table1 has two rows with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.