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 3850738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:03:22+00:00 2026-05-19T17:03:22+00:00

I find myself often wanting to write Python list comprehensions like this: nearbyPoints =

  • 0

I find myself often wanting to write Python list comprehensions like this:

nearbyPoints = [(n, delta(n,x)) for n in allPoints if delta(n,x)<=radius]

That hopefully gives some context as to why I would want to do this, but there
are also cases where multiple values need to be computed/compared per
element:

newlist = [(x,f(x),g(f(x))) for x in bigList if f(x)<p and g(f(x))<q]

So I have two questions:

  1. will all those functions be evaluated multiple times or is the result cached?
    Does the language specify or is it implementation-specific? I’m using 2.6 now, but would 3.x be different?
  2. is there a neater way to write it? Sometimes f and g are long expressions and
    duplication is error prone and looks messy. I would really like to be able to
    write this:
newList = [(x,a=f(x),b=g(a)) for x in bigList if a<p and b<q]

but that doesn’t work. Is there a good reason for not supporting this syntax? Can
it be done via something like this?
Or would I just have to use multiple listcomps or a for-loop?

  • 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-05-19T17:03:22+00:00Added an answer on May 19, 2026 at 5:03 pm

    In regards to #1, yes, they will be evaluated multiple times.

    In regards to #2, the way to do it is to calculate and filter in separate comprehensions:

    Condensed version:

    [(x,fx,gx) for (x,fx,gx) in ((x,fx,g(fx)) for (x,fx) in ((x,f(x)) for x in bigList) if fx < p) if gx<q]
    

    Longer version expanded to make it easier to follow:

    [(x,f,g) for (x,f,g) in
      ((x,f,g(f)) for (x,f) in
         ((x,f(x)) for x in bigList)
      if f < p)
    if g<q]
    

    This will call f and g as few times as possible (values for each f(x) is not < p will never call g, and f will only be called once for each value in bigList).

    If you prefer, you can also get neater code by using intermediate variables:

    a = ( (x,f(x)) for x in bigList )
    b = ( (x,fx,g(fx)) for (x,fx) in a if fx<p )
    results = [ c for c in b if c[2] < q ] # faster than writing out full tuples
    

    a and b use generator expressions so that they don’t have to actually instantiate lists, and are simply evaluated when necessary.

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

Sidebar

Related Questions

I often find myself wanting to write an SQL query like the following: SELECT
Often, I find myself wanting to write a unit test for a portion of
I often find myself writing a property that is evaluated lazily. Something like: if
Quite often, I find myself wanting a simple, dump object in Python which behaves
I often find myself wanting to debug CSS layout issues that involve DOM changes
I seem to often find myself wanting to store data of more than one
EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text) ? null : Convert.ToInt32(employeeNumberTextBox.Text), I often find myself wanting to do
While creating classes in Java I often find myself creating instance-level collections that I
I often find myself storing data in a mysql database, and then wanting to
I often find myself wanting to open Xcode and do a little coding on

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.