I'll assume i in {created_comprehension}
was meant to be i not in {created_comprehension}
. At least that's what the data suggests.
So here's a fun horrible abuse that I wouldn't trust to always work. Mainly to demonstrate that the argument "it's impossible because it's not yet assigned" is wrong. While the list object indeed isn't assigned yet, it does already exist while it's being built.
>>> import gc
>>> [i if i not in self else 0
for ids in [set(map(id, gc.get_objects()))]
for self in [next(o for o in gc.get_objects() if o == [] and id(o) not in ids)]
for i in [1, 2, 1, 2, 3]]
[1, 2, 0, 0, 3]
This gets the ids of all objects tracked for garbage collection before the new list gets created, and then after it got created, we find it by searching for a newly tracked empty list. Call it self
and then you can use it. So the middle two lines are a general recipe. I also successfully used it for this question, but it got closed before I could post.
A nicer version:
>>> [i if i not in self else 0
for old in [ids()] for self in [find(old)]
for i in [1, 2, 1, 2, 3]]
[1, 2, 0, 0, 3]
That used these helper functions:
def ids():
import gc
return set(map(id, gc.get_objects()))
def find(old):
import gc
return next(o for o in gc.get_objects() if o == [] and id(o) not in old)