Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:15:59+00:00 2026-06-09T00:15:59+00:00

Possible Duplicate: sorted() using Generator Expressions Rather Than Lists We all know using generators

  • 0

Possible Duplicate:
sorted() using Generator Expressions Rather Than Lists

We all know using generators instead of instantiating lists all the time saves time and memory, especially if we use comprehensions a lot.

Here’s a question though, consider the following code:

output = SomeExpensiveCallEgDatabase()
results = [result[0] for result in output]
return sorted(results)

The call to sorted will return a sorted list of the results. Would it be better or worse to declare results as below and then call sorted?

results = (result[0] for result in output)

My guess is the call to sorted() would traverse the generator and instantiate a list itself in order to run quicksort or mergesort on it. So there would be no advantage in using the generator here. Is this assumption correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:16:02+00:00Added an answer on June 9, 2026 at 12:16 am

    I believe your assumption to be true, since there is no easy way of ordering the collection without first having the whole list in memory (at least certainly not with the default sorting algorithm, TimSort if I’m not mistaken).

    Check this out:
    sorted() using Generator Expressions Rather Than Lists

    To create the new List, the builtin sorted method uses PySequence_List:

    PyObject* PySequence_List(PyObject *o) Return value: New reference.
    Return a list object with the same contents as the arbitrary sequence
    o. The returned list is guaranteed to be new.

    Pros and cons of both approaches:

    Memory-wise:

    The returned list is the one used for the sorted version, so this would mean that in this case, only one list is stored completely in memory at any given time, using the generator version.

    This makes the generator version more efficient memory-wise.

    Speed:

    Here the version with the whole list wins.

    To create a new list based on a generator, an empty list must be created (or at best with the first element), and each following element appended to the list, with the possible redimensioning steps this may provoke.

    To create a new list based on a previous list, the size of the list is known beforehand, and thus can be allocated at once and each of the entries assigned (possibly, there are other optimizations at work here, but I can’t back that up).

    So regarding speed, the list wins.

    The answer to “what’s the best”, comes down to the most common answer in any field of engineering… it depends….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: finding all numbers less than x in a BST How would I
Possible Duplicate: Sorting list of lists by a third list of specified non-sorted order
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Python analog of natsort function (sort a list using a “natural order”
Possible Duplicate: Find the min number in all contiguous subarrays of size l of
Possible Duplicate: Import XML to SQL using C# I just downloaded about 3000 XML
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of
Possible Duplicate: Sorted ArrayList not displaying? Code: String title1 = Original Order\n\n; String title2
Possible Duplicate: Given a 2d array sorted in increasing order from left to right
Possible Duplicate: Interview Q: sorting an almost sorted array (elements misplaced by no more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.