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
Python program to find Maximum and minimum element’s position in a list?
In Python, Lists are one of the most versatile data types. They store sequences of comma-separated items enclosed in square brackets []. Finding the maximum and minimum element positions is a common task that can be achieved using Python's built-in functions max(), min(), and index().
Understanding the Required Functions
max() Function
The max() function returns the largest element in a list. For numbers, comparison is done numerically; for strings, comparison is done alphabetically.
max(list)
min() Function
The min() function returns the smallest element in a list. Like max(), it compares numbers numerically and strings alphabetically.
min(list)
index() Method
The index() method returns the lowest index where a specified element appears in the list.
list.index(element, start, end)
Finding Maximum and Minimum Positions
The approach is straightforward: first find the max and min values using max() and min(), then use index() to get their positions.
Example 1: Basic List
Let's find the max and min element positions in a simple list ?
numbers = [1, 3, 5, 7]
max_value = max(numbers)
min_value = min(numbers)
max_index = numbers.index(max_value)
min_index = numbers.index(min_value)
print("Max value:", max_value, "at Index:", max_index)
print("Min value:", min_value, "at Index:", min_index)
Max value: 7 at Index: 3 Min value: 1 at Index: 0
Example 2: List with Negative Numbers
Here's how it works with negative numbers ?
numbers = [-4, -6, 2, 4, 6]
max_value = max(numbers)
min_value = min(numbers)
max_index = numbers.index(max_value)
min_index = numbers.index(min_value)
print("Max value:", max_value, "at Index:", max_index)
print("Min value:", min_value, "at Index:", min_index)
Max value: 6 at Index: 4 Min value: -6 at Index: 1
Example 3: List with Identical Elements
When all elements are the same, index() returns the first occurrence ?
numbers = [2, 2, 2, 2, 2]
max_value = max(numbers)
min_value = min(numbers)
max_index = numbers.index(max_value)
min_index = numbers.index(min_value)
print("Max value:", max_value, "at Index:", max_index)
print("Min value:", min_value, "at Index:", min_index)
Max value: 2 at Index: 0 Min value: 2 at Index: 0
Compact Solution
You can combine the operations into fewer lines ?
numbers = [10, 5, 8, 20, 3]
max_index = numbers.index(max(numbers))
min_index = numbers.index(min(numbers))
print(f"Maximum element {max(numbers)} is at index {max_index}")
print(f"Minimum element {min(numbers)} is at index {min_index}")
Maximum element 20 is at index 3 Minimum element 3 is at index 4
Key Points
| Function | Purpose | Returns |
|---|---|---|
max() |
Find largest element | Maximum value |
min() |
Find smallest element | Minimum value |
index() |
Find element position | First occurrence index |
Conclusion
Finding maximum and minimum positions in Python lists is straightforward using max(), min(), and index() functions. Remember that index() always returns the first occurrence when duplicate values exist.
