[Tutor] What's the difference between sort(aList) and aList.sorted()

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jul 26 19:36:16 EDT 2017


On 26/07/17 19:40, C W wrote:

> My understanding of each is:
> 1) function(variable) is manipulating a vector, I can do bList =
> sorted(aList)
> 2) object.method() is permanently changing it, I don't even need to assign
> it in #1.
> 
> Why is there both? They do the same thing.

As you have just shown they do very different things.
One sorts the list in place the other returns a sorted
copy of the list.

Sometimes you want one, sometimes the other.


-- 
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