 Open in App
Not now

# Python | Inserting item in sorted list maintaining order

• Difficulty Level : Basic
• Last Updated : 01 Feb, 2023

Working with sorted list is essential because mostly the data we get is ordered. Any query can come to insert the new data into its appropriate position. Hence requires to know how to perform these dynamic queries. Lets discuss certain ways in which this can be performed.

Method #1 : Naive Method In this method, we just simply test for the value and check if next element is greater than the input element and store the index which is then used to slice in that element at that position.

## Python3

 `# Python3 code to demonstrate``# insertion in sorted list``# using naive method` `# initializing list``test_list ``=` `[``1``, ``2``, ``3``, ``6``, ``7``]` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# insert element``k ``=` `5` `# using naive method``# insertion in sorted list``# using naive method``for` `i ``in` `range``(``len``(test_list)):``    ``if` `test_list[i] > k:``        ``index ``=` `i``        ``break``res ``=` `test_list[: i] ``+` `[k] ``+` `test_list[i :]` `# printing result``print` `(``"The list after insertion is : "` `+` `str``(res))`

Output

```The original list is : [1, 2, 3, 6, 7]
The list after insertion is : [1, 2, 3, 5, 6, 7]```

Time complexity: O(n) where n is size of  list
Auxiliary Space: O(n)

Method #2 : Using bisect.insort() This is more concise and recommended method to perform this particular task. This method is designed for this particular task and performs this task in most efficient manner.

## Python3

 `# Python3 code to demonstrate``# insertion in sorted list``# using bisect.insort()``import` `bisect` `# initializing list``test_list ``=` `[``1``, ``2``, ``3``, ``6``, ``7``]` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# insert element``k ``=` `5` `# using bisect.insort()``# insertion in sorted list``# using naive method``bisect.insort(test_list, k)` `# printing result``print` `(``"The list after insertion is : "` `+` `str``(test_list))`

Output

```The original list is : [1, 2, 3, 6, 7]
The list after insertion is : [1, 2, 3, 5, 6, 7]```

Time complexity: O(n) where n is size of given list
Auxiliary space: O(1)

Method #3 : Using heapq

To insert an element into a sorted list using a min heap , you can use the following code:

## Python3

 `import` `heapq` `test_list ``=` `[``1``, ``2``, ``3``, ``6``, ``7``]``k ``=` `5``heap ``=` `test_list ``+` `[k]``heapq.heapify(heap)``sorted_list ``=` `[heapq.heappop(heap) ``for` `_ ``in` `range``(``len``(heap))]``print``(sorted_list)``#This code is contributed by Edula Vinay Kumar Reddy`

Output

`[1, 2, 3, 5, 6, 7]`

This code first creates a min heap from the elements of test_list and the element k using the heapq.heapify() function. Then, it extracts the elements from the heap one by one using a list comprehension and the heapq.heappop() function, and stores them in a new list sorted_list, which is then printed.

This approach has a time complexity of O(n log n), since the heapq.heapify() and heapq.heappop() functions have a time complexity of O(n) and O(log n), respectively. It also has a space complexity of O(n), since it requires an additional heap data structure to store the elements.

Method #4 : Using append() and sort() methods

## Python3

 `# Python3 code to demonstrate``# insertion in sorted list` `# initializing list``test_list ``=` `[``1``, ``2``, ``3``, ``6``, ``7``]` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# insert element``k ``=` `5` `test_list.append(k)``test_list.sort()``# printing result``print` `(``"The list after insertion is : "` `+` `str``(test_list))`

Output

```The original list is : [1, 2, 3, 6, 7]
The list after insertion is : [1, 2, 3, 5, 6, 7]```

Time Complexity : O(N log N)

Auxiliary Space : O(1)

Method #5 : .insert()

## Python3

 `from` `bisect ``import` `bisect_left``test_list ``=` `[``1``, ``2``, ``3``, ``6``, ``7``]``k ``=` `5``index ``=` `bisect_left(test_list, k)``test_list.insert(index, k)``print``(test_list)`

Output

`[1, 2, 3, 5, 6, 7]`

Time Complexity : O(N)

Auxiliary Space : O(1)

Method #6: Using list comprehension

## Python3

 `test_list ``=` `[``1``, ``2``, ``3``, ``6``, ``7``]``# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))``k ``=` `5``res ``=` `[x ``for` `x ``in` `test_list ``if` `x < k] ``+` `[k] ``+` `[x ``for` `x ``in` `test_list ``if` `x >``=` `k]``print``(res)``#This code is contributed by Vinay pinjala.`

Output

```The original list is : [1, 2, 3, 6, 7]
[1, 2, 3, 5, 6, 7]```

Time Complexity : O(N)

Auxiliary Space : O(N)

My Personal Notes arrow_drop_up