snippetpythonTip
Index of min or max element
Viewed 0 times
minelementindexmaxpython
Problem
To find the index of the element with the minimum or maximum value in a list, you can use the
min() and max() functions to get the minimum or maximum value in the list, and then use the list.index() method to find the index of that value.Solution
def min_element_index(arr):
return arr.index(min(arr))
def max_element_index(arr):
return arr.index(max(arr))
min_element_index([3, 5, 2, 6, 10, 7, 9]) # 2
max_element_index([3, 5, 2, 6, 10, 7, 9]) # 4Code Snippets
def min_element_index(arr):
return arr.index(min(arr))
def max_element_index(arr):
return arr.index(max(arr))
min_element_index([3, 5, 2, 6, 10, 7, 9]) # 2
max_element_index([3, 5, 2, 6, 10, 7, 9]) # 4Context
From 30-seconds-of-code: min-max-element-index
Revisions (0)
No revisions yet.