Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the Index of Maximum Item in a List using Python
In Python, finding the index of the maximum item in a list is a common task. Python provides several built-in functions like max(), index(), and enumerate() that can be used to find the index of the maximum item in a list.
For example, given the list [61, 12, 4, 5, 89, 7], the maximum value is 89 at index 4.
Using index() with max()
The simplest approach combines max() to find the maximum value and index() to find its position ?
numbers = [61, 12, 4, 5, 89, 7]
max_index = numbers.index(max(numbers))
print(f"Maximum value: {max(numbers)}")
print(f"Index of maximum value: {max_index}")
Maximum value: 89 Index of maximum value: 4
Using enumerate() with max()
Use enumerate() with max() and a custom key function to find both value and index in one pass ?
numbers = [61, 12, 4, 5, 89, 7]
max_index, max_value = max(enumerate(numbers), key=lambda x: x[1])
print(f"Maximum value: {max_value}")
print(f"Index of maximum value: {max_index}")
Maximum value: 89 Index of maximum value: 4
Using Manual Loop
A manual approach using a for loop to track both maximum value and its index ?
numbers = [61, 12, 4, 5, 89, 7]
max_value = numbers[0]
max_index = 0
for i in range(1, len(numbers)):
if numbers[i] > max_value:
max_value = numbers[i]
max_index = i
print(f"Maximum value: {max_value}")
print(f"Index of maximum value: {max_index}")
Maximum value: 89 Index of maximum value: 4
Using Pandas (for larger datasets)
For data analysis tasks, Pandas provides idxmax() to find the index of maximum value ?
import pandas as pd
numbers = [61, 12, 4, 5, 89, 7]
series = pd.Series(numbers)
max_index = series.idxmax()
max_value = series.max()
print(f"Maximum value: {max_value}")
print(f"Index of maximum value: {max_index}")
Maximum value: 89 Index of maximum value: 4
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
index(max()) |
O(n) | Simple, readable code |
enumerate() + max() |
O(n) | Single pass through list |
| Manual loop | O(n) | Educational purposes |
Pandas idxmax()
|
O(n) | Data analysis tasks |
Handling Edge Cases
For lists with duplicate maximum values, these methods return the first occurrence ?
numbers = [89, 12, 4, 5, 89, 7] # Two maximum values
max_index = numbers.index(max(numbers))
print(f"Index of first maximum value: {max_index}")
# To find all indices of maximum values
max_val = max(numbers)
all_max_indices = [i for i, x in enumerate(numbers) if x == max_val]
print(f"All indices of maximum values: {all_max_indices}")
Index of first maximum value: 0 All indices of maximum values: [0, 4]
Conclusion
Use list.index(max(list)) for simple cases. Use enumerate() with max() for better performance when you need both value and index. For data analysis, prefer Pandas idxmax() method.
