[Tutor] sorted function

Steven D'Aprano steve at pearwood.info
Fri Apr 14 20:14:56 EDT 2017


On Fri, Apr 14, 2017 at 11:59:25PM +0530, shubham goyal wrote:

>   sorted(ls)
>   sorted(ls1)

Here you sort ls and throw the result away, then you do the same to ls1.

sorted() makes a copy of the list and sorts it. You need to write:

ls = sorted(ls)
ls1 = sorted(ls1)

but even better would be to sort in place:

ls.sort()
ls1.sort()

which doesn't make a copy.



-- 
Steve


More information about the Tutor mailing list