HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonTip

Index of min or max element

Submitted by: @import:30-seconds-of-code··
0
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]) # 4

Code 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]) # 4

Context

From 30-seconds-of-code: min-max-element-index

Revisions (0)

No revisions yet.