I am using the .remove()
function to remove an element from a list and then I want to use directly this new list into another function e.g. the print()
function. When I am doing this then I get the following:
print([1, 2, 3])
# [1, 2, 3]
print([1, 2, 3].remove(2))
# None
So if I want to use directly another function (e.g. print()
) after an Inplace function such as .remove()
then paradoxically I can only do this like this:
print([1, 2, 3])
# [1, 2, 3]
x = [1, 2, 3]
x.remove(2)
print(x)
# [1, 3]
Is there any better way to do this instead of writing this additional source code?
Apparently the same problem applies for all Inplace functions as attributes of other functions I guess.
You can create a wrapper function for list remove and call that function instead of calling list.remove directly.
def list_remove(l, elem):
l.remove(elem)
return l
print (list_remove(l, elem))
Disclaimer: I really don't recommend you to do this. But, this is just a method that came to my mind.
I really don't see a problem in writing list.remove()
and print
in separate statements.