0

I have two lists as below and I am trying to figure out which index one list's items would be if they were in the other list (a little difficult to explain without an example).

list_1 = [35, 101, 128]

list_2 = [5, 27, 53, 73, 93, 120, 147]

So what I am trying to do is to take list_1's values and say, for example, that the value 35 would have been the 3rd value in list_2 (index = 2) and that 101 would be the 6th value (index = 5) in list_2, etc. Seems like there should be an easier way to do it than what I am currently thinking.

Jimmy
  • 127
  • 10
  • There's nothing built-in that does this. – Barmar Oct 06 '22 at 20:29
  • is `list_2` always going to be sorted? – juanpa.arrivillaga Oct 06 '22 at 20:30
  • 1
    @juanpa.arrivillaga The question makes absolutely no sense if it's not. – Barmar Oct 06 '22 at 20:30
  • 2
    @Barmar on the contrary: what OP is looking for is the index of where a value would be inserted into ascending sorted data, which is exactly what `bisect.bisect_left` does out of box (Or `bisect.bisect` / `bisect.bisect_right`, depending on whether/how we care about ties being broken). I closed this as a duplicate of the best question I could easily find about the `bisect` module. Similarly there are `insort` functions that will actually insert the value directly. – Karl Knechtel Oct 06 '22 at 20:31
  • @KarlKnechtel Good catch, I forget about the bisect library. – Barmar Oct 06 '22 at 20:32

0 Answers0