After seeing the discussion here: Python - generate the time difference I got curious. I also initially thought that a generator is faster than a list, but when it comes to sorted() I don't know. Is there any benefit to sending a generator expression to sorted() rather than a list? Does the generator expression end up being made into a list inside sorted() before sorting anyway?
EDIT: It grieves me to only be able to accept one answer, as I feel a lot of responses have helped to clarify the issue. Thanks again to everyone.
The primary difference between the two is that list. sort() will sort the list in-place, mutating its indexes and returning None , whereas sorted() will return a new sorted list leaving the original list unchanged.
sorted simply always returns a new list object from an arbitrary iterable. The type of the iterable is irrelevant.
Thus, generator expressions are faster than list comprehension and hence time efficient.
The Python sorted() uses the Timsort algorithm which is a hybrid sorting algorithm, derived from merge sort and insertion sort.
The first thing sorted()
does is to convert the data to a list. Basically the first line (after argument validation) of the implementation is
newlist = PySequence_List(seq);
See also the full source code version 2.7 and version 3.1.2.
Edit: As pointed out in the answer by aaronasterling, the variable newlist
is, well, a new list. If the parameter is already a list, it is copied. So a generator expression really has the advantage of using less memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With