-2

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.

Outcast
  • 4,967
  • 5
  • 44
  • 99
  • The observed behavior is correct. `[1, 2, 3].remove(2)` modifies the list inplace but returns nothing. This can be verified by just typing `[1, 2, 3].remove(2)` in a terminal and you will see no output. So, unless you stores it into a new variable (or the same variable as the list), `print()` will just show `None`. – Sheldore Jan 17 '19 at 12:15
  • Well the functions are *in-place*, why would you expect them to return something? – Dani Mesejo Jan 17 '19 at 12:19
  • Adding to below answers, you can create a custom "remove" function, that prints the newlist after the removed element. Check [this](https://www.geeksforgeeks.org/python-print-list-removing-element-given-index/). – miguelarc Jan 17 '19 at 12:27
  • How is this "paradoxical" or "problematic"? You have two very short and clear lines of code, I just don't see what your issue is? – Chris_Rands Jan 17 '19 at 12:29
  • @Chris_Rands, I find it paradoxical in the sense that an in-place function obliges you to not use it as an attribute in another function "in-place" (meaning in the same statement) while for me using a in-place function benefits you in exactly not writing new statements. – Outcast Jan 17 '19 at 12:30
  • Some in-place functions do return a value, e.g. `list.pop()`, but in general a function should *either* mutate something *or* have a return value of interest, not both, see the command-query separation principle. Anyway, if `list.remove` did return a value, it would certainly be the value removed and not the remaining list https://en.wikipedia.org/wiki/Command%E2%80%93query_separation – Chris_Rands Jan 17 '19 at 12:33

3 Answers3

0

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.

Jay
  • 24,173
  • 25
  • 93
  • 141
0

If you really want one statement without modifying the list in question, you could try my_list[:my_list.index(value)]+my_list[my_list.index(value)+1:]. This has the drawbacks

  • you calculate my_list.index(value) twice (will not be neccessary 3.8+, see PEP572)
  • you slice and add, which is much more than just removing
  • it isn't really (near) as clear as my_list.remove(val)

And,


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.

--Jay

Community
  • 1
  • 1
user24343
  • 892
  • 10
  • 19
0

You can wrap it into a one-liner using a lambda, if you really want to:

print( (lambda x,y:x if x.remove(y) is None else None) ([1, 2, 3], 2) )

Do you need a copy of the Disclaimer from the other two answers? In short: it is not a very readable piece of code.

tevemadar
  • 12,389
  • 3
  • 21
  • 49