Let's say that we have a simple code:
chars = ["a", "a", "b"]
added = []
for c in chars:
if c in added:
added.append(c + '_added_again')
else:
added.append(c)
# added is ['a', 'a_added_again', 'b']
We are calling added
that is being extend inside loop.
The question is, is it possible to reproduce this behavior with list comprehension?
Something like
chars = ["a", "a", "b"]
added = [
c + '_added_again' if c in {something like `self` or `this` here, or maybe lambda can be used?} else c for c in chars
]
And same question about map
. Is it possible to access list that is still under construction inside map
function?
added = list(map(lambda x: x + '_added_again' if ??? else x, chars))
Why I asked this? Just for curiosity.