Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorted() using generator expressions rather than lists

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.

like image 506
Brent Newey Avatar asked Nov 11 '10 12:11

Brent Newey


People also ask

What is the difference between list sort () and sorted list?

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.

Does sorted always return a list?

sorted simply always returns a new list object from an arbitrary iterable. The type of the iterable is irrelevant.

Is generator faster than list?

Thus, generator expressions are faster than list comprehension and hence time efficient.

What does sorted () use in Python?

The Python sorted() uses the Timsort algorithm which is a hybrid sorting algorithm, derived from merge sort and insertion sort.


1 Answers

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.

like image 192
Sven Marnach Avatar answered Oct 13 '22 23:10

Sven Marnach