Index of Max Element

PythonPythonBeginner
Practice Now

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

Introduction

In this challenge, you will create a function that takes a list of numbers as an argument and returns the index of the element with the maximum value.


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-13687{{"`Index of Max Element`"}} python/lists -.-> lab-13687{{"`Index of Max Element`"}} python/tuples -.-> lab-13687{{"`Index of Max Element`"}} python/function_definition -.-> lab-13687{{"`Index of Max Element`"}} python/build_in_functions -.-> lab-13687{{"`Index of Max Element`"}} end

Index of Max Element

Write a function max_element_index(arr) that takes a list arr as an argument and returns the index of the element with the maximum value. If there are multiple elements with the maximum value, return the index of the first occurrence.

To solve this problem, you can follow these steps:

  1. Use the built-in max() function to find the maximum value in the list.
  2. Use the built-in list.index() function to find the index of the first occurrence of the maximum value in the list.
  3. Return the index.
def max_element_index(arr):
  return arr.index(max(arr))
max_element_index([5, 8, 9, 7, 10, 3, 0]) ## 4

Summary

In this challenge, you learned how to find the index of the element with the maximum value in a list. You used the built-in max() and list.index() functions to solve the problem.

Other Python Tutorials you may like