why does list's .remove() does not return an object?
Dan Strohl
D.Strohl at F5.com
Thu May 17 12:28:05 EDT 2018
On 2018-05-17 11:26 AM, Abdur-Rahmaan Janhangeer wrote:
> I don't understand what this would return? x? You already have x. Is
> it meant to make a copy? x has been mutated, so I don't understand the
> benefit of making a copy of the 1-less x. Can you elaborate on the
> problem you are trying to solve?
>
> --Ned.
>
>
> assignment to another var
>
Though I don’t know what the OP was specifically looking for I could see a benefit to returning the item deleted.
So, lets take as an example I have an object like:
class ListItem(object):
def __init__(self, key, data):
self.key = key
self.data = data
def __eq__(other):
return other == self.key
and I do something like:
i1 = ListItem('hello', 'foobar')
l2 = ListItem('goodby', 'snafu')
l = [i1, i2]
So, lets say I have a need where I want to do something like a remove, but I also want to be able to get the .data variable from the object I am removing, it would be nice to be able to simply do
x = l.remove('hello')
print(x.data)
Yes, I could do a index/pop to get this, or I could keep a separate dict of the objects as well for lookups, or a number of other techniques, but it would be easier to simply get it back during the remove().
Dan Strohl
More information about the Python-list
mailing list