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

  • Home
  • SEARCH
  • 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 7822681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:59:32+00:00 2026-06-02T07:59:32+00:00

If I do this: x=[(t,some_very_complex_computation(y)) for t in z] Apparently some_very_complex_computation(y) is not dependent

  • 0

If I do this:

x=[(t,some_very_complex_computation(y)) for t in z]

Apparently some_very_complex_computation(y) is not dependent on t. So it should be evaluated only once. Is there any way to make Python aware of this, so it won’t evaluate some_very_complex_computation(y) for every iteration?

Edit: I really want to do that in one line…

  • 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-02T07:59:33+00:00Added an answer on June 2, 2026 at 7:59 am

    Usually you should follow San4ez’s advise and just use a temporary variable here. I will still present a few techniques that might prove useful under certain circumstances:

    In general, if you want to bind a name just for a sub-expression (which is usually why you need a temporary variable), you can use a lambda:

    x = (lambda result=some_very_complex_computation(y): [(t, result) for t in z])()
    

    In this particular case, the following is a quite clean and readable solution:

    x = zip(z, itertools.repeat(some_very_complex_computation(y)))
    

    A general note about automatic optimizations like these

    In a dynamic language like Python, an implementation would have a very hard time to figure out that some_very_complex_computation is referentially transparent, that is, that it will always return the same result for the same arguments. You might want to look into a functional language like Haskell if you want magic like that.

    “Explicit” pureness: Memoization

    What you can do however is make some_very_complex_computation explicitly cache its return values for recent arguments:

    from functools import lru_cache
    
    @lru_cache()
    def some_very_complex_computation(y):
      # ...
    

    This is Python 3. In Python 2, you’d have to write the decorator yourself:

    from functools import wraps
    
    def memoize(f):
      cache = {}
      @wraps(f)
      def memoized(*args):
        if args in cache:
          return cache[args]
        res = cache[args] = f(*args)
        return res
      return memoized
    
    @memoize
    some_very_complex_computation(x):
      # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should reduce the executable size quite a bit in some of my very
This should be a very basic design question, but for some reason it doesn't
I want to have URLs like this: some-company-name-deal-id-6.htm So the rewrite rule should ignore
Warning! This problem is not for the feint of heart. I've spent several days
I know this question might be repeated many times but would really appreciate any
I want to make my xml layout more modular because this one is going
These last weeks, I found myself using a lot of const everywhere. Not only
There are a few threads here at so about this matter but most of
I have a JPanel and I want do draw to this JPanel some very
Ok, I asked about this very error earlier this week and had some very

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.