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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:13:48+00:00 2026-06-08T10:13:48+00:00

Consider the following list comprehension [ (x,f(x)) for x in iterable if f(x) ]

  • 0

Consider the following list comprehension

[ (x,f(x)) for x in iterable if f(x) ]

This filters the iterable based a condition f and returns the pairs of x,f(x). The problem with this approach is f(x) is calculated twice.
It would be great if we could write like

[ (x,fx) for x in iterable if fx where fx = f(x) ]
or
[ (x,fx) for x in iterable if fx with f(x) as fx ]

But in python we have to write using nested comprehensions to avoid duplicate call to f(x) and it makes the comprehension look less clear

[ (x,fx) for x,fx in ( (y,f(y) for y in iterable ) if fx ]

Is there any other way to make it more pythonic and readable?


Update

Coming soon in python 3.8! PEP

# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]
  • 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-08T10:13:50+00:00Added an answer on June 8, 2026 at 10:13 am

    You seek to have let-statement semantics in python list comprehensions, whose scope is available to both the ___ for..in(map) and the if ___(filter) part of the comprehension, and whose scope depends on the ..for ___ in....


    Your solution, modified:
    Your (as you admit unreadable) solution of [ (x,fx) for x,fx in ( (y,f(y) for y in iterable ) if fx ] is the most straightforward way to write the optimization.

    Main idea: lift x into the tuple (x,f(x)).

    Some would argue the most “pythonic” way to do things would be the original [(x,f(x)) for x in iterable if f(x)] and accept the inefficiencies.

    You can however factor out the ((y,fy) for y in iterable) into a function, if you plan to do this a lot. This is bad because if you ever wish to have access to more variables than x,fx (e.g. x,fx,ffx), then you will need to rewrite all your list comprehensions. Therefore this isn’t a great solution unless you know for sure you only need x,fx and plan to reuse this pattern.


    Generator expression:

    Main idea: use a more complicated alternative to generator expressions: one where python will let you write multiple lines.

    You could just use a generator expression, which python plays nicely with:

    def xfx(iterable):
        for x in iterable:
            fx = f(x)
            if fx:
                yield (x,fx)
    
    xfx(exampleIterable)
    

    This is how I would personally do it.


    Memoization/caching:

    Main idea: You could also use(abuse?) side-effects and make f have a global memoization cache, so you don’t repeat operations.

    This can have a bit of overhead, and requires a policy of how large the cache should be and when it should be garbage-collected. Thus this should only be used if you’d have other uses for memoizing f, or if f is very expensive. But it would let you write…

    [ (x,f(x)) for x in iterable if f(x) ]
    

    …like you originally wanted without the performance hit of doing the expensive operations in f twice, even if you technically call it twice. You can add a @memoized decorator to f: example (without maximum cache size). This will work as long as x is hashable (e.g. a number, a tuple, a frozenset, etc.).


    Dummy values:

    Main idea: capture fx=f(x) in a closure and modify the behavior of the list comprehension.

    filterTrue(
        (lambda fx=f(x): (x,fx) if fx else None)() for x in iterable
    )
    

    where filterTrue(iterable) is filter(None, iterable). You would have to modify this if your list type (a 2-tuple) was actually capable of being None.

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

Sidebar

Related Questions

Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following criteria query: var x = SomeCriteria.AddOrder(new Order(Name, true)).List(); This will order
I've always been confused about this one. Consider the following loops: int [] list
Consider the following situation where I have a list of n matrices (this is
Consider the following problem: given a list of length three of tuples (String,Int), is
Consider the following code: List<double> l = new List<double>(); //add unknown number of values
Consider the following data structure: List<Person> People; class Person { List<Car> Cars; List<Hobby> Hobbies;
Consider the following code and assume that list is an synchronized List. List list
Consider the following C# code: [XmlRoot] public class A { [XmlArray] public List<B> ArrayOfBItems
Consider following code: My problem is: 1) I can't seem to cast the errors

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.