why does list's .remove() does not return an object?

Ned Batchelder ned at nedbatchelder.com
Thu May 17 12:25:41 EDT 2018


On 5/17/18 11:57 AM, Abdur-Rahmaan Janhangeer wrote:
> x = [0,1]
> x.remove(0)
> new_list = x
>
> instead i want in one go
>
> x = [0,1]
> new_list = x.remove(0) # here a way for it to return the modified list by
> adding a .return() maybe ?

There isn't a way to do that in one line.  I often find myself splitting 
long statements into more, shorter, statements to express myself more 
clearly.

I don't know if you have a real piece of code in mind, so I don't know 
if you can tell us:  why is it useful to have another variable referring 
to the same list?

--Ned.
>
> Abdur-Rahmaan Janhangeer
> https://github.com/Abdur-rahmaanJ
>
> On Thu, 17 May 2018, 19:54 Alexandre Brault, <abrault at mapgears.com> wrote:
>
>> 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
>>>
>> You already have access to the list before removal, the list after
>> removal and the element to be removed.
>>
>> Do need a copy of the list before removing x?
>>>>> old_list = list[:]
>>>>> list.remove(x)
>> Do you need the list after removing x?
>>>>> list.remove(x)  # list is the modified list
>> Do you need x?
>>>>> list.remove(x)  # x is x
>> What else would need to be assigned to another var?
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>




More information about the Python-list mailing list