You are viewing a single comment's thread. Return to all comments →
Can anyone plz explain why sorted() works and not the function sort() ?
My understanding is that sorted() returns a new sorted list, and also works for things besides lists (map objects, tuples, etc.). list.sort() sorts a list in place.
You can use sort() it is efficient than sorted() as it does in-place sorting. They used sorted, so that they write it in lesser number of characters.
sort() returns None whereas sorted() returns the newly created list.
Using sort() :
nums.sort() #here in-place sorting takes place
'set' object has no attribute 'sort'
^ This. The set function doesn't create a list, and sets don't allow for the sort attribute. Using sorted() converts the data into a list, then sorts it.