To remove an element and assign the list in one line
Solution 1:
I suppose this would work:
a = [1, 3, 2, 4]
b = (a.remove(1), a)[1]
This is assuming that you want to do both things:
- Modify the original list by removing the element, and
- Return the (now modified) list.
EDIT
Another alternative:
b = a.remove(1) or a
Both are fairly confusing; I'm not sure I would use either in code.
EDIT 2
Since you mentioned wanting to use this in a lambda... are you aware that you can use an actual named function any time you would otherwise use a lambda?
E.g. instead of something like this:
map(lambda x: x.remove(1) and x, foo)
You can do this:
def remove_and_return(x):
x.remove(1)
return x
map(remove_and_return, foo)
Solution 2:
Since remove
returns None
, you could just or a
to it:
>>> a = [1, 3, 2, 4]
>>> a.remove(1) or a
[3, 2, 4]