Index of Max Element | Challenge

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

Index of Max Element

Problem

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.

Example

max_element_index([5, 8, 9, 7, 10, 3, 0]) ## 4

In this example, the maximum value in the list [5, 8, 9, 7, 10, 3, 0] is 10, which occurs at index 4. Therefore, the function should return 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