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

  • Home
  • SEARCH
  • 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 295843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:29:39+00:00 2026-05-12T06:29:39+00:00

I would like to add an element to a list that preserve the order

  • 0

I would like to add an element to a list that preserve the order of the list.

Let’s assume the list of object is [a, b, c, d] I have a function cmp that compares two elements of the list. if I add f object which is the bigger I would like it to be at the last position.

maybe it’s better to sort the complete list…

  • 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-05-12T06:29:39+00:00Added an answer on May 12, 2026 at 6:29 am

    bisect.insort is a little bit faster, where applicable, than append-then-sort (unless you have a few elements to add before you need the list to be sorted again) — measuring as usual on my laptop (a speedier machine will of course be faster across the board, but the ratio should remain roughly constant):

    $ python -mtimeit -s'import random, bisect; x=range(20)' 'y=list(x); bisect.insort(y, 22*random.random())'
    1000000 loops, best of 3: 1.99 usec per loop
    

    vs

    $ python -mtimeit -s'import random, bisect; x=range(20)' 'y=list(x); y.append(22*random.random()); y.sort()'
    100000 loops, best of 3: 2.78 usec per loop
    

    How much you care about this difference, of course, depend on how critical a bottleneck this operation is for your application — there are of course situation where even this fraction of a microsecond makes all the difference, though they are the exception, not the rule.

    The bisect module is not as flexible and configurable — you can easily pass your own custom comparator to sort (although if you can possibly put it in the form of a key= argument you’re strongly advised to do that; in Python 3, only key= remains, cmp= is gone, because the performance just couldn’t be made good), while bisect rigidly uses built-in comparisons (so you’d have to wrap your objects into wrappers implementing __cmp__ or __le__ to your liking, which also has important performance implications).

    In your shoes, I’d start with the append-then-sort approach, and switch to the less-handy bisect approach only if profiling showed that the performance hit was material. Remember Knuth’s (and Hoare’s) famous quote, and Kent Beck’s almost-as-famous one too!-)

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

Sidebar

Related Questions

I have a List<class> that I would like to convert into a json object
I would like to override the List object in C# in order to add
I have an img element with style='width:40%;height:40%;' . I would like to add a
I would like to have an arrayList that holds reference to object inside the
I would like to be able to add a non-selectable element to a list
Let's say we have a list of elements: [{dog,1},{dog,2},{cat,1},{cat,2},{bird,1},{bird,2},...] I would like to store
I would like to add an element to jquery.colorbox.js(want to do some fancey styling)
I would like to add to an element's position a dynamic amount e.g. 40px
I would like to add a class which renders an element a particular color
I have own project and i would like add for this class same as

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.