[Tutor] What's the difference between sort(aList) and aList.sorted()
Joel Goldstick
joel.goldstick at gmail.com
Wed Jul 26 18:37:11 EDT 2017
On Wed, Jul 26, 2017 at 2:40 PM, C W <tmrsg11 at gmail.com> wrote:
> Dear Python experts,
>
> I suppose I have the following Python code:
>
> aList = [3, 5, 2, 4]
>
> sorted(aList)
> > [2, 3, 4, 5]
>
> aList.sort()
>
> aList
> > [2, 3, 4, 5]
>
> 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. Is if I unknowingly hit the
> keyboard with the aList.sort(), then the "damage" is permanent.
>
> Thank you!
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
I haven't looked it up recently, but one sorts in place (object.method) i
think, and the other (a function) returns a new sorted list
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
More information about the Tutor
mailing list