Through indexing, you can retrieve an item or range of items from a list. You can only retrieve a specific item from a list if you know the index number associated with that value.
To find the index value of the largest item in a list, you can use the max()
and index()
methods. This guide walks you through a program that retrieves the index value of the maximum element in a list.
Python: Retrieve the Index Value of the Max Value in a List
Write a program that retrieves the name of the student who earned the highest grade on their last math test. This name is then printed to the console.
To begin, lcreate two lists:
students = ["Andy", "April", "Julia", "Gary"] grades = [75, 82, 64, 68]
The first list contains the names of the students in the class. The second list contains their respective grades.
These lists are parallel arrays, which means that both of the arrays are the same size and each value represents part of a record. “students” contains names and “grades” contains the grades those students have earned.
Next, find the largest grade in the list. You can use the max() function to find the largest grade in the list:
largest = max(grades)
This code retrieves the largest number. This is not exactly ideal. You want to retrieve the index position of the largest number. To do that, employ the use of another built-in function: index(). Let’s retrieve the index value of the largest item in the list:
index_of_largest = grades.index(largest)
Because the two lists are parallel, the index number of the highest grade in the “grades” list is the same as the index number of the student who earned that grade in the “students” list.
Next, print a message to the console which tells us who earned the highest grade:
print("{} earned the highest grade in the class. Their grade was {}.".format(students[index_of_largest], largest))
Use indexing syntax to retrieve the name of the student who earned the highest grade. Then add this value and the grade they earned into a string.
Run the program and see what happens:
April earned the highest grade in the class. Their grade was 82.
The code successfully identifies the student who has earned the highest grade. To find the index of minimum elements, you could substitute the max()
function for min()
.
Find Multiple Indexes Equal to Max Value
What happens if there are two students who have the highest grade? The above program only returns the index position of the first student who has earned a particular grade.
To find the occurrences of all students who have earned a particular grade, you can use a list comprehension. Start by initializing the values and retrieving the index value of the largest item in the list like earlier:
students = ["Andy", "April", "Julia", "Gary", "Paul"] grades = [75, 82, 64, 68, 82] largest = max(grades)
You’ve added one record to see what happens if two students have the same grade in the new program.
Next, write a list comprehension. This list comprehension finds the index positions of all the students who earned a particular grade:
all_grades = [g for g, x in enumerate(grades) if x == largest]
This code iterates through every grade in the “grades” list. Each grade is compared to the largest value in the “grades” list. If a value is equal to the highest grade, it is added to the list.
The enumerate()
function splits our list into two lists: index numbers and values. Thus, “g” represents the index of an item in the list and “x” represents the value of an item.
Next, use a for loop to print out a message for each student whose grade is equal to the highest grade found:
for g in all_grades: print("{} earned the highest grade in the class. Their grade was {}.".format(students[g], grades[g]))
If multiple students have earned the highest grade in the class, the for loop will run multiple times. Each time the loop runs, the name of a student is printed to the console with a message saying they earned the highest grade in the class.
Run our program:
April earned the highest grade in the class. Their grade was 82. Paul earned the highest grade in the class. Their grade was 82.
Our program successfully shows the names of all the students who earned the highest grade in the class.
Conclusion
You can retrieve the index value of an item in a list using index()
. Combined with the min()
or max()
method, you can find the index value of the smallest or the largest item in a list.
Now you have the knowledge you need to retrieve the index value of the max value in a list like a Python expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.