Index of min element

PythonPythonBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13694{{"`Index of min element`"}} python/lists -.-> lab-13694{{"`Index of min element`"}} python/tuples -.-> lab-13694{{"`Index of min element`"}} python/function_definition -.-> lab-13694{{"`Index of min element`"}} python/build_in_functions -.-> lab-13694{{"`Index of min element`"}} end

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.

Other Python Tutorials you may like