The Python Standard Library: Data Structures

• Print
This chapter is from the book

2.4 bisect—Maintain Lists in Sorted Order

• Purpose Maintains a list in sorted order without having to call sort each time an item is added to the list.
• Python Version 1.4 and later

The bisect module implements an algorithm for inserting elements into a list while maintaining the list in sorted order. For some cases, this is more efficient than repeatedly sorting a list or explicitly sorting a large list after it is constructed.

2.4.1 Inserting in Sorted Order

Here is a simple example using insort() to insert items into a list in sorted order.

```import bisect
import random

# Use a constant seed to ensure that
# the same pseudo-random numbers
# are used each time the loop is run.
random.seed(1)

print 'New  Pos  Contents'
print '---  ---  --------'

# Generate random numbers and
# insert them into a list in sorted
# order.
l = []
for i in range(1, 15):
r = random.randint(1, 100)
position = bisect.bisect(l, r)
bisect.insort(l, r)
print '%3d %3d' % (r, position), l```

The first column of the output shows the new random number. The second column shows the position where the number will be inserted into the list. The remainder of each line is the current sorted list.

```\$ python bisect_example.py

New  Pos  Contents
---  ---  --------
14    0 [14]
85    1 [14, 85]
77    1 [14, 77, 85]
26    1 [14, 26, 77, 85]
50    2 [14, 26, 50, 77, 85]
45    2 [14, 26, 45, 50, 77, 85]
66    4 [14, 26, 45, 50, 66, 77, 85]
79    6 [14, 26, 45, 50, 66, 77, 79, 85]
10    0 [10, 14, 26, 45, 50, 66, 77, 79, 85]
3    0 [3, 10, 14, 26, 45, 50, 66, 77, 79, 85]
84    9 [3, 10, 14, 26, 45, 50, 66, 77, 79, 84, 85]
44    4 [3, 10, 14, 26, 44, 45, 50, 66, 77, 79, 84, 85]
77    9 [3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]
1    0 [1, 3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]```

This is a simple example, and for the amount of data being manipulated, it might be faster to simply build the list and then sort it once. But for long lists, significant time and memory savings can be achieved using an insertion sort algorithm such as this one.

2.4.2 Handling Duplicates

The result set shown previously includes a repeated value, 77. The bisect module provides two ways to handle repeats. New values can be inserted to the left of existing values or to the right. The insort() function is actually an alias for insort_right(), which inserts after the existing value. The corresponding function insort_left() inserts before the existing value.

```import bisect
import random

# Reset the seed
random.seed(1)

print 'New  Pos  Contents'
print '---  ---  --------'

# Use bisect_left and insort_left.
l = []
for i in range(1, 15):
r = random.randint(1, 100)
position = bisect.bisect_left(l, r)
bisect.insort_left(l, r)
print '%3d %3d' % (r, position), l
```

When the same data is manipulated using bisect_left() and insort_left(), the results are the same sorted list, but the insert positions are different for the duplicate values.

```\$ python bisect_example2.py

New  Pos  Contents
---  ---  --------
14    0 [14]
85    1 [14, 85]
77    1 [14, 77, 85]
26    1 [14, 26, 77, 85]
50    2 [14, 26, 50, 77, 85]
45    2 [14, 26, 45, 50, 77, 85]
66    4 [14, 26, 45, 50, 66, 77, 85]
79    6 [14, 26, 45, 50, 66, 77, 79, 85]
10    0 [10, 14, 26, 45, 50, 66, 77, 79, 85]
3    0 [3, 10, 14, 26, 45, 50, 66, 77, 79, 85]
84    9 [3, 10, 14, 26, 45, 50, 66, 77, 79, 84, 85]
44    4 [3, 10, 14, 26, 44, 45, 50, 66, 77, 79, 84, 85]
77    8 [3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]
1    0 [1, 3, 10, 14, 26, 44, 45, 50, 66, 77, 77, 79, 84, 85]```

In addition to the Python implementation, a faster C implementation is available. If the C version is present, that implementation automatically overrides the pure Python implementation when bisect is imported.