Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
As far as I know, the list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list.

Can I convert it back to the unsorted state after the list.sort() has been implemented?

1 Answer

0 votes
by (108k points)

Kindly be informed that the sorted() function will return a new sorted list, leaving the original list unchanged. Whereas the list.sort() will sort the list in-place, mutating the list indices, and returns None (like all in-place operations).

sorted() works on any iterable, not just lists. When you want to mutate the list, you can work with list.sort(), use sorted() when you want a new sorted object back. Use sorted() when you want to sort something that is iterable, not a list yet.

One more interesting thing is that the list.sort() is faster than sorted() because it doesn't have to create a copy. For any other iterable, you have no choice.

No, you cannot retrieve the original positions. Once you called the list.sort() the original order is gone.

For the best of career growth, check out Intellipaat’s Python online Course and get certified!

Related questions

Browse Categories

...