3 ms·
The benefit is only lost if you want to store the sorted mylist. However, sorted() is useful if you want to create a list, sort it, use it, and throw it away..
by dbecker 14y ago
The benefit is only lost if you want to store the sorted mylist.
However, sorted() is useful if you want to create a list, sort it, use it, and throw it away... all in a single line.
The blog gives an example where a file is read, sorted and transformed. If he loaded the file to a list and sorted that list before transforming it, there would be an unnecessary copy of the list sitting around consuming memory.