Re: why does list's .remove() does not return an object?
ah there's no return question then that precisely returned the discussion to the first answer Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ On Thu, 17 May 2018, 23:35 Tobiah,wrote: > On 05/17/2018 09:25 AM, Ned Batchelder wrote: > > On 5/17/18 11:57 AM, Abdur-Rahmaan Janhangeer wrote: > >> x = [0,1] > >> x.remove(0) > >> new_list = x > > Just call the original list 'new_list' to begin with. > >new_list = [0, 1] >new_list.remove(0) > > > There you are! > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Aw: Re: why does list's .remove() does not return an object?
>new_list = list(x.remove(0)) >new_list = x.remove(0)[:] Please disregard :) kh -- https://mail.python.org/mailman/listinfo/python-list
Re: Re: why does list's .remove() does not return an object?
On Thu, May 17, 2018 at 3:27 PM, Karsten Hilbertwrote: >> 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. > >new_list = list(x.remove(0)) >new_list = x.remove(0)[:] > > ? > > No one said x can't be modified, only that new_list is > to contain the modified list after one line of code :) Did you actually run either of those? -- https://mail.python.org/mailman/listinfo/python-list
Aw: Re: why does list's .remove() does not return an object?
> 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. new_list = list(x.remove(0)) new_list = x.remove(0)[:] ? No one said x can't be modified, only that new_list is to contain the modified list after one line of code :) kh -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
On 05/17/2018 09:25 AM, Ned Batchelder wrote: On 5/17/18 11:57 AM, Abdur-Rahmaan Janhangeer wrote: x = [0,1] x.remove(0) new_list = x Just call the original list 'new_list' to begin with. new_list = [0, 1] new_list.remove(0) There you are! -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
On 17-May-2018 12:37, Ned Batchelder wrote: > On 5/17/18 12:28 PM, Dan Strohl via Python-list 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 > > > > > Though I don’t know what the OP was specifically looking for I could see a > > benefit to returning the item deleted. > > Notice that this is not the thing the OP wanted returned. I believe they > are looking to return the list, not the item. > I'm guessing that they want to be able to do some sort of method chaining like: the_list.remove(x).remove(y) Although the clarifying example was contrived and confusing. A more concrete example would be greatly appreciated. -- david stanek web: https://dstanek.com twitter: https://twitter.com/dstanek -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
On 5/17/18 12:28 PM, Dan Strohl via Python-list 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 Though I don’t know what the OP was specifically looking for I could see a benefit to returning the item deleted. Notice that this is not the thing the OP wanted returned. I believe they are looking to return the list, not the item. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
RE: why does list's .remove() does not return an object?
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
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,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 -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
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 ? Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ On Thu, 17 May 2018, 19:54 Alexandre Brault,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 > -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
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
Re: why does list's .remove() does not return an object?
On Thu, 17 May 2018, 18:55 Ned Batchelder,wrote: > On 5/17/18 4:23 AM, Abdur-Rahmaan Janhangeer wrote: > > if then a more convenient way might be found to naturally remove and > return the list > > maybe it was not included as one might want to remove the list only > > x = [1] > x.remove(1) > > as opposed to > > x = [1] > x.remove(1) > new_list = x > > i was looking for like > > x = [1] > x.remove(1).return() > > > 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 > > -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
On 5/17/18 4:23 AM, Abdur-Rahmaan Janhangeer wrote: if then a more convenient way might be found to naturally remove and return the list maybe it was not included as one might want to remove the list only x = [1] x.remove(1) as opposed to x = [1] x.remove(1) new_list = x i was looking for like x = [1] x.remove(1).return() 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. (PS: bottom-posting (adding your response below the text you are responding to) will make the conversation easier to follow...) ps. list is was demo illustrative var Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ On Thu, 17 May 2018, 07:01 Ned Batchelder,> wrote: On 5/16/18 10:41 PM, Abdur-Rahmaan Janhangeer wrote: > why is x = list.remove(elem) not return the list? > > Methods in Python usually do one of two things: 1) mutate the object and return None; or 2) leave the object alone and return a new object. This helps make it clear which methods mutate and which don't. Since .remove mutates the list, it returns None. --Ned. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
Abdur-Rahmaan Janhangeer at 2018/5/17 PM 04:23 wrote: if then a more convenient way might be found to naturally remove and return the list maybe it was not included as one might want to remove the list only x = [1] x.remove(1) as opposed to x = [1] x.remove(1) new_list = x IMO, this way is more flexible on its usage and avoid a redundant copy. --Jach i was looking for like x = [1] x.remove(1).return() ps. list is was demo illustrative var Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ On Thu, 17 May 2018, 07:01 Ned Batchelder,wrote: On 5/16/18 10:41 PM, Abdur-Rahmaan Janhangeer wrote: why is x = list.remove(elem) not return the list? Methods in Python usually do one of two things: 1) mutate the object and return None; or 2) leave the object alone and return a new object. This helps make it clear which methods mutate and which don't. Since .remove mutates the list, it returns None. --Ned. -- https://mail.python.org/mailman/listinfo/python-list --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
if then a more convenient way might be found to naturally remove and return the list maybe it was not included as one might want to remove the list only x = [1] x.remove(1) as opposed to x = [1] x.remove(1) new_list = x i was looking for like x = [1] x.remove(1).return() ps. list is was demo illustrative var Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ On Thu, 17 May 2018, 07:01 Ned Batchelder,wrote: > On 5/16/18 10:41 PM, Abdur-Rahmaan Janhangeer wrote: > > why is x = list.remove(elem) not return the list? > > > > > Methods in Python usually do one of two things: 1) mutate the object and > return None; or 2) leave the object alone and return a new object. This > helps make it clear which methods mutate and which don't. Since .remove > mutates the list, it returns None. > > --Ned. > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
On Wednesday, May 16, 2018 at 7:42:01 PM UTC-7, Abdur-Rahmaan Janhangeer wrote: > why is x = list.remove(elem) not return the list? > > Abdur-Rahmaan Janhangeer > https://github.com/Abdur-rahmaanJ 1) If you are naming your list "list," you're asking for trouble. Shadowing builtin names is risky. Ten years ago I named a variable "max". Oops. 2) list.remove() operates in-place. Would you expect it to return a copy? Making that copy could potentially use a lot of memory and time. 3) If you do want a copy, construct it from two slices of your original list. Let's say the element you want to remove is at position 6. Then: new_list = old_list[:6] + old_list[7:] 3) -- https://mail.python.org/mailman/listinfo/python-list
Re: why does list's .remove() does not return an object?
On 5/16/18 10:41 PM, Abdur-Rahmaan Janhangeer wrote: why is x = list.remove(elem) not return the list? Methods in Python usually do one of two things: 1) mutate the object and return None; or 2) leave the object alone and return a new object. This helps make it clear which methods mutate and which don't. Since .remove mutates the list, it returns None. --Ned. -- https://mail.python.org/mailman/listinfo/python-list