Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created December 23, 2019 17:54
Show Gist options
  • Save luisenriquecorona/c86a82e53dfdd6279759f27509880791 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/c86a82e53dfdd6279759f27509880791 to your computer and use it in GitHub Desktop.
Sorting is expensive, so once you have a sorted sequence, it’s good to keep it that way. That is why bisect.insort was created.
import bisect
import random
SIZE = 7
random.seed(1729)
my_list = []
for i in range(SIZE):
new_item = random.randrange(SIZE*2)
bisect.insort(my_list, new_item)
print('%2d ->' % new_item, my_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment