I have a sorted list
my_list = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]
I have a value lets say 445. I want to find the index of first max in list. For above case, it should return index value 9 (512).
The easiest way is to use binary search to find the required index, because the input list is sorted. This algorithm is already implemented in the bisect module:
import bisect
my_list = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]
print(bisect.bisect_right(my_list, 445))
print(bisect.bisect_right(my_list, 512))
Prints:
9
10
for i, v in enumerate(my_list):to populate both index and value.