# How to find the index of an element in a list which is minimum in a sublist of that list in Python?

2632 views
6

Assume I have a Python list:

x = [3,6,4,8,1,9]

and I want to find the index of the element in the list which is the minimum inside a sublist (say from index 2 to 4).

So I want to take a sublist x[2:5] and get the index of the minimum element (in this case x).

How to return index 4 in such a case? If I use np.argmin() on the sublist, it will return the index according to the sublist (in this case, np.argmin(x[2:5]) will return 2 which is correct according to the sublist).

I don't want to use multiple if-else conditions. How to go about getting this index in a short way?

0

Just add the index where the sublist starts, and you'll have your original index:

``````x = [3,6,4,8,1,9]
subl_start = 2
subl_end = 5

ind_min = np.argmin(x[subl_start: subl_end]) + subl_start
``````

posted this
9

Add the starting index of your sublist to `np.argmin` to get minimum index on full list:

``````x = [3,6,4,8,1,9]

np.argmin(x[2:5])+2

4
``````

posted this