# Bisect Module in Python

In this tutorial, we will learn about the bisect algorithm, which is written in Python. Its source code is written within 80 lines. Let's go through the introduction to the bisect module.

## Introduction

It is basically a bisection algorithm to find the insertion point for adding a given element in the sorted list. However, it is crucial to remember that its functions take the sorted list as an argument. The list must be sorted in ascending order. If the passed list is not in ascending order, the function will not throw an error, but the result might be unexpected. Now let's understand its methods.

## Important Methods

Let's understand some essential methods of bisect methods.

• bisect_left(a, x, lo=0, hi=None) -

It returns the index i where must be inserted the value x such that given restore its order. If value x already appears in the list, the index i would be just before the leftmost x value is already there. Let's understand the following example.

Example -

Output:

```The element to be inserted at place:  2
```

Explanation -

In the above code, we have defined the list a, and we want to insert 45. We want to insert 45 so that a is kept ordered. We called the bisect_left() method, which returned the 2; it is where the given value must be inserted.

Considering the above snippet, we try to insert x value in the list a by calling the insert() method of the object list.

Example - 2:

Output:

```[23, 40, 45, 61, 82, 100, 12, 14]
```

Explanation -

As shown in the above code, we have inserted a value at index 3. Let's see another example where we will use the bisects that the Bisect Module provides.

Now, we will insert the element that is already present in the list.

Additionally, we will use the lo and hi parameters. Let's understand the following example -

Example - 3:

Output:

```The element is to be insorted at: 3
[23, 40, 61, 82, 82, 100, 12, 14]
```

Explanation -

As we mentioned earlier, the bisect_left method corresponds to the list's first match of the value x (82). The lo and hi parameters are used to specify the starting and ending index of the list that we want to consider. If we don't pass these parameters, the whole list is considered, as seen in the earlier example. Let's understand the example of lo and hi.

Example - 4:

Output:

```The element is to be inserted at: 3
[23, 40, 61, 82, 82, 100, 12, 14]
```

Explanation -

As lo and hi were passed to bisect_left() method the list to consider it would be a[2:7] what corresponds to [23, 40, 61, 82, 82, 100, 12, 14]. The bisect_left finds the index i in this slice and then returns el index based on the whole list.

• insort_right(a, x, lo=0, hi=None)

It works the same as the insort_left(), except that it uses the bisect_right() function to find where the x value needs to be inserted. Let's understand the following example.

Example -

Output:

```[10, 20, 30, 40, 40, 40, 70, 60]
```

If we use the bisect_right method, we will get the same result.

It is the best way to use the function of the Bisect Module. It is essential to specify that the time complexity of the bisect_left and bisect_right is O(log(n)) since their implementation uses Binary Search Algorithm.

However, the time complexity of the insort_left and insort_right depends on the complexity of the insert method. The time complexity of these methods is O(n).

In the above examples, we have used the integers, but we can also use the list of floats or strings.

But what if we use a tuple or a string? We can use the bisect_right() and bisect_left(). But if we try to use the insort_left and insort_right on a string or tuple, we will get an error. Because the tuples and strings are immutable objects, these objects do not have implemented the insert method, which modifies one immutable object as a list in place.

The bisect_right and insort_right have aliases, so bisect_right can be called bisect and insort_right as insort.

## Problems Related to Bisect Module

Let's solve some interesting problems using the bisect module.

Problem - Write the Python program to find three integers which gives the sum of zero in a given array using the Binary Search (bisect)

Example -

Output:

```[[-40, 0, 40], [-20, -20, 40], [-20, 0, 20]]
[[-6, 1, 5], [-6, 2, 4]]
```

Problem -2: Write a Python program to find the first occurrence of a given number in a sorted list using Binary Search (bisect).

Solution

Output:

```First occurrence of 3 is present at index 2
```

Problem - 3: Write a Python program to find the index position of the last occurrence of a given number in a sorted list using Binary Search (bisect).

Example -

Output:

```Last occurrence of 3 is present at index 3
```

## Conclusion

In the above tutorial, we have a detailed discussion over the bisect module and its methods. We have seen some examples and learned about the bisect_right and bisect_left methods. This module is used to solve binary search-related problems.

### Feedback   