Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
What is the difference between insort_left and insort_right in bisect?

Why do both insort_left and insort_right exist; isn't it always the same result since the elements are equal?

>>> import bisect
>>> foo = [1,2,3]
>>> 
>>> bisect.insort_left(foo, 1)
>>> foo
[1, 1, 2, 3]
>>> 
>>> bisect.insort_right(foo, 1)
>>> foo
[1, 1, 1, 2, 3]
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Objects can be equivalent without being identical.

>>> bisect.insort_left(foo, 1.0)
>>> foo
[1.0, 1, 1, 1, 2, 3]
>>> 
>>> bisect.insort_right(foo, 1.0)
>>> foo
[1.0, 1, 1, 1, 1.0, 2, 3]
over 4 years ago · Santiago Trujillo Report

0

For most purposes the results are indistinguishable, but there are cases where it can matter, particularly when using the optional key= argument.

Do you understand the difference between sorting algorithms that are, or aren't, guaranteed to be "stable"? If not, click the link ;-)

ys = []
for x in xs:
    bisect.insort_right(ys, x)

fills ys with a stable sort of xs entries, but using insort_left() instead would not.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!