In this tutorial, we will look at how to find the max value in a Python list and its corresponding index with the help of some examples.

## How to get the maximum value in a list in Python?

A simple approach is to iterate through the list and keep track of the max value. Alternatively, you can also use the Python built-in `max()`

function to find the maximum value in a list.

Let’s look at some of the different ways of finding the maximum value and its index in a list.

### Loop through the list to find the maximum

Iterate through the list values and keep track of the maximum value. Here’s an example.

# create a list ls = [3, 1, 7, 2, 6, 5] # find max value using loop max_val = ls[0] for val in ls: if val > max_val: max_val = val # display the max value print(max_val)

Output:

7

Here, we iterate over each value in the list `ls`

and keep track of the maximum value encountered in the variable `max_val`

. After the loop finishes, the variable `max_val`

stores the maximum value present in the list, 7.

You can use this method to get the index of the maximum value present in the list as well. Use an additional variable to keep track of the current maximum value’s index.

# create a list ls = [3, 1, 7, 2, 6, 5] # find max value using loop max_val = ls[0] max_val_idx = 0 for i in range(len(ls)): if ls[i] > max_val: max_val = ls[i] max_val_idx = i # display the max value print(max_val) # display its index print(max_val_idx)

Output:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

7 2

We get the maximum value and its index after the loop finishes. Here we iterate through the list via its index rather than the values. You can also use the `enumerate()`

function to iterate through the index and value together.

### Using `max()`

to get the maximum value

You can also use the Python built-in `max()`

function to get the max value in a list. The function returns the maximum value in the passed iterable (for example, list, tuple, etc.).

# create a list ls = [3, 1, 7, 2, 6, 5] # find max value max(ls)

Output:

7

Using the `max()`

function is simple and is just a single line code compared to the previous example.

You can use the list `index()`

function to find the index corresponding to the maximum value (assuming you already know the maximum value).

# create a list ls = [3, 1, 7, 2, 6, 5] # find max value max_val = max(ls) # display the max value print(max_val) # display its index print(ls.index(max_val))

Output:

7 2

We get the max value and its index in the list `ls`

.

Note that the list `index()`

function returns the index of the first occurrence of the passed value. If the max value occurs more than once in the list, you’ll only get the index of its first occurrence. You can use list comprehension to get all the indices of occurrence of the max value in the list.

# create a list ls = [3, 1, 7, 2, 7, 5] # find max value max_val = max(ls) print(max_val) # find all indices corresponding max val max_val_idx = [i for i in range(len(ls)) if ls[i]==max_val] print(max_val_idx)

Output:

7 [2, 4]

We get all the indices where the max value occurs in the list `ls`

.

You might also be interested in –

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**