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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:29:30+00:00 2026-05-20T07:29:30+00:00

In sum: I need to write a List Comprehension in which i refer to

  • 0

In sum: I need to write a List Comprehension in which i refer to list that is being created by the List Comprehension.

This might not be something you need to do every day, but i don’t think it’s unusual either.

Maybe there’s no answer here–still, please don’t tell me i ought to use a for loop. That might be correct, but it’s not helpful. The reason is the problem domain: this line of code is part of an ETL module, so performance is relevant, and so is the need to avoid creating a temporary container–hence my wish to code this step in a L/C. If a for loop would work for me here, i would just code one.

In any event, i am unable to write this particular list comprehension. The reason: the expression i need to write has this form:

[ some_function(s) for s in raw_data if s not in this_list ]

In that pseudo-code, “this_list” refers to the list created by evaluating that list comprehension. And that’s why i’m stuck–because this_list isn’t built until my list comprehension is evaluated, and because this list isn’t yet built by the time i need to refer to it, i don’t know how to refer to it.

What i have considered so far (and which might be based on one or more false assumptions, though i don’t know exactly where):

  • doesn’t the python interpreter have
    to give this list-under-construction
    a name? i think so

  • that temporary name is probably taken
    from some bound method used to build
    my list (‘sum’?)

  • but even if i went to the trouble to
    find that bound method and assuming
    that it is indeed the temporary name
    used by the python interpreter to
    refer to the list while it is under
    construction, i am pretty sure you
    can’t refer to bound methods
    directly; i’m not aware of such an
    explicit rule, but those methods (at
    least the few that i’ve actually
    looked at) are not valid python
    syntax. I’m guessing one reason why
    is so that we do not write them into
    our code.

so that’s the chain of my so-called reasoning, and which has led me to conclude, or at least guess, that i have coded myself into a corner. Still i thought i ought to verify this with the Community before turning around and going a different direction.

  • 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-20T07:29:31+00:00Added an answer on May 20, 2026 at 7:29 am

    There used to be a way to do this using the undocumented fact that while the list was being built its value was stored in a local variable named _[1].__self__. However that quit working in Python 2.7 (maybe earlier, I wasn’t paying close attention).

    You can do what you want in a single list comprehension if you set up an external data structure first. Since all your pseudo code seemed to be doing with this_list was checking it to see if each s was already in it — i.e. a membership test — I’ve changed it into a set named seen as an optimization (checking for membership in a list can be very slow if the list is large). Here’s what I mean:

    raw_data = [c for c in 'abcdaebfc']
    
    seen = set()
    def some_function(s):
        seen.add(s)
        return s
    
    print [ some_function(s) for s in raw_data if s not in seen ]
    # ['a', 'b', 'c', 'd', 'e', 'f']
    

    If you don’t have access to some_function, you could put a call to it in your own wrapper function that added its return value to the seen set before returning it.

    Even though it wouldn’t be a list comprehension, I’d encapsulate the whole thing in a function to make reuse easier:

    def some_function(s):
        # do something with or to 's'...
        return s
    
    def add_unique(function, data):
        result = []
        seen = set(result) # init to empty set
        for s in data:
            if s not in seen:
                t = function(s)
                result.append(t)
                seen.add(t)
        return result
    
    print add_unique(some_function, raw_data)
    # ['a', 'b', 'c', 'd', 'e', 'f']
    

    In either case, I find it odd that the list being built in your pseudo code that you want to reference isn’t comprised of a subset of raw_data values, but rather the result of calling some_function on each of them — i.e. transformed data — which naturally makes one wonder what some_function does such that its return value might match an existing raw_data item’s value.

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

Sidebar

Related Questions

No related questions found

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.