0

I wonder If, in Python, is there any way to get the index of an element that is not in the list, i.e the index in which this element should be.

I explain better: Supposing that I have the following sorted list A:

A = [2, 4, 7, 11, 26, 41]

and I want to know which should be the index of the element 9 which is not in the list, I would like to get something like this:

index: 3

since in the sorted list A the element 9 would be the element at postion 3.

Is there any way to do so? Maybe by implementing a function?

  • 1
    You mean identify the index at which you could insert it to maintain sort order? https://docs.python.org/3/library/bisect.html#bisect.bisect_left - `bisect_left([2, 4, 7, 11, 26, 41], 9) == 3`. – jonrsharpe Jul 05 '21 at 16:07

0 Answers0