Why doesn’t the following statement return a list without 'item' in Python?
list(something_convertible_to_list).remove('item')
?
I would like to use the construction above to avoid explicitly naming a list for the sole purpose of passing it to a function, i.e.:
operate_on_list(list(something_convertible_to_list).remove('item'))
def operate_on_list(my_list):
# do_something
print my_list
return
Is this possible in Python?
In python, built-in methods which operate in place return
Noneto make it absolutely clear that they mutated an object.Of course, you’re free to disregard this convention and write your own wrapper:
But I wouldn’t recommend it.
As a side note, if you want to do something like:
but get the list back, the following might be similar enough to be useful:
And this does return a list, but where
list.removeonly takes away 1'item', this will construct a new list with no'item'in it.