Index of min element

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In Python, you can easily find the minimum value in a list using the min() function. However, what if you also need to know the index of that minimum value? In this challenge, you will create a function that returns the index of the element with the minimum value in a list.

Index of min element

Write a function min_element_index(arr) that takes a list of integers arr as an argument and returns the index of the element with the minimum value in the list.

To solve this problem, you can use the min() function to obtain the minimum value in the list and then use the list.index() method to return its index.

def min_element_index(arr):
  return arr.index(min(arr))
min_element_index([3, 5, 2, 6, 10, 7, 9]) ## 2

Summary

In this challenge, you learned how to find the index of the element with the minimum value in a list using Python. By using the min() function and the list.index() method, you can easily obtain the index of the minimum value in a list.