[Tutor] Python program to remove first four even numbers from a list

Alan Gauld alan.gauld at yahoo.co.uk
Sun Oct 17 15:35:33 EDT 2021


On 17/10/2021 16:03, Manprit Singh wrote:
> Dear sir,
> 
> By writing this :
> lst[:] = remove4even(lst)   # here i am modify the existing list lst
> 
> But by doing this
> 
> lst = remove4even(lst)     #  Now variable lst is reassigned, so it is a
> different object

But an identical object. And the first method copies the object
returned from the function before throwing it away whereas the
direct assignment is much less processor (and time) intensive
and achieves the same result.

The slicing approach would be the best one if you wanted to
replace part of the original list with the returned values,
but if you are going to replace the whole list you might
as well just reassign the variable.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list