sorted() using generator expressions rather than lists
optimization, python
Solution
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.
Problem
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.