DEV Community

221910303015
221910303015

Posted on

Difference between sorted and sort in python

Most of the people confuse when to use sorted and sort in python,as we see both are used to sort a list.
The basic difference between sorted() and sort() function is sorted function is use to create a new list which has the elements of sorted version whereas sort function is used to modifiy the list it is called on.If the user wants to have the unsorted orginal version after sorted it he should use sorted() function. If user does not give any importance to orginial list he can use sort() function.
Coming to its syntax :
sorted() : new_list = sorted(orginial_list)
sort() : orginial_list.sort()

Top comments (0)