max value in a list

Python – Find Max Value and its Index in List

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.

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.

max value in a list

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

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

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top