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 98529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:12:48+00:00 2026-05-11T00:12:48+00:00

I need to get the lesser n numbers of a list in Python. I

  • 0

I need to get the lesser n numbers of a list in Python. I need this to be really fast because it’s in a critical part for performance and it needs to be repeated a lot of times.

n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the function. Sorting can’t be made in place.

Initially, I have written this function:

def mins(items, n):     mins = [float('inf')]*n     for item in items:         for i, min in enumerate(mins):             if item < min:                 mins.insert(i, item)                 mins.pop()                 break     return mins 

But this function can’t beat a simple sorted(items)[:n] which sort the entire list. Here is my test:

from random import randint, random import time  test_data = [randint(10, 50) + random() for i in range(20000)]  init = time.time() mins = mins(test_data, 8) print 'mins(items, n):', time.time() - init  init = time.time() mins = sorted(test_data)[:8] print 'sorted(items)[:n]:', time.time() - init 

Results:

mins(items, n): 0.0632939338684 sorted(items)[:n]: 0.0231449604034 

sorted()[:n] is three times faster. I believe this is because:

  1. insert() operation is costly because Python lists are not linked lists.
  2. sorted() is an optimized c function and mine is pure python.

Is there any way to beat sorted()[:n] ? Should I use a C extension, or Pyrex or Psyco or something like that?

Thanks in advance for your answers.

  • 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. 2026-05-11T00:12:49+00:00Added an answer on May 11, 2026 at 12:12 am

    You actually want a sorted sequence of mins.

    mins = items[:n] mins.sort() for i in items[n:]:     if i < mins[-1]:          mins.append(i)         mins.sort()         mins= mins[:n] 

    This runs much faster because you aren’t even looking at mins unless it’s provably got a value larger than the given item. About 1/10th the time of the original algorithm.

    This ran in zero time on my Dell. I had to run it 10 times to get a measurable run time.

    mins(items, n): 0.297000169754 sorted(items)[:n]: 0.109999895096 mins2(items)[:n]: 0.0309998989105 

    Using bisect.insort instead of append and sort may speed this up a hair further.

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

Sidebar

Related Questions

i have PostsController controller and in this controller i need get all user list.
I need to find values inside this string: 10days25hours15minutes I need get the numbers
<div class=myDiv> <p>I need get<strong>this</strong> <a title=And this href=#>but not this</a> </p> <p>And also<strong>This</strong>
I need get the same type of array that this.form.elements returns, with input names
I need get the encoding of a web page(UTF-8,ISO-8859-1,etc) before I download it because
is it possible to do this RewriteRule robots.txt dirname(%{DOCUMENT_ROOT})/robots.php [L] i need get parent
I have a string. there I need get a one word. this word is
I need get it independent if the network connection is active or not. I
I need get position element in table. But function position() returns me to the
Just need get some vals located in application.ini(main ini) in the Controller plugin I

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.