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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:53:11+00:00 2026-06-05T15:53:11+00:00

These days I design some algorithms in python, but find first two greatest value

  • 0

These days I design some algorithms in python, but find first two greatest value in python is too ugly and inefficient.

How to implement it in a efficient or a pythonic way?

  • 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-05T15:53:12+00:00Added an answer on June 5, 2026 at 3:53 pm

    I’ve found this to be consistently faster (about 2x for a list of 1,000,000 items) than heapq.nlargest:

    def two_largest(sequence):
        first = second = 0
        for item in sequence:
            if item > second:
                if item > first:
                    first, second = item, first
                else:
                    second = item
        return first, second
    

    (function modified at the suggestion of MatthieuW)

    Here are the results of my testing (timeit was taking forever, so I used time.time()):

    >>> from random import shuffle
    >>> from time import time
    >>> seq = range(1000000)
    >>> shuffle(seq)
    >>> def time_it(func, *args, **kwargs):
    ...     t0 = time()
    ...     func(*args, **kwargs)
    ...     return time() - t0
    ...
    
    >>> #here I define the above function, two_largest().
    >>> from heapq import nlargest
    >>> time_it(nlargest, 2, seq)
    0.258958101273
    >>> time_it(two_largest, seq)
    0.145977973938
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

These days, we encountered a strange problem, some of our solr apps on tomcat
I've searched for two days trying to find a solution to this. My problem
I have developed web applications using JSF (myfaces components). But in these days of
I 've a question related to design of file systems. These days we are
These days I have took Peter Norvig's Udacity course CS212: DESIGN OF COMPUTER PROGRAMS.
Hi I am learning Design patterns these days. I want to read design pattern
Currently I am working on the design of my degree project. Some days ago
These days I am trying to design architecture of a new MMORPG mobile game
These days I'm learning design patterns. There are a lot of documentation about programming
These days when I create a new repository on GitHub on the setup page

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.