Python | Program to find the position of minimum and maximum elements of a list

Position of minimum and maximum elements of a Python list: In this tutorial, we will learn how to find and print the position/index of the minimum and maximum elements of a Python list. And, we will write a program to solve this problem. By IncludeHelp Last updated : June 22, 2023

Prerequisite

Problem Statement

Given a Python list and we have to find the index/position of the minimum and maximum elements of a list.

Consider the below example with the sample input/output values:

Input:
list = [10, 1, 2, 20, 3, 20]

Output:
Positive of minimum element:  1
Positive of maximum element:  3 

How to find the position of minimum and maximum elements of a Python list?

To find the positions/indexes of minimum and maximum elements of a list, we need to find the maximum and minimum elements of the list – to find the maximum element of the list, we will use max(list) and to find the minimum element of the list, we will use min(list).

To get the positions/indices of these minimum and maximum elements, you can use the list.index() method. Thus, the statements to get the positions will be list.index(max(list)) and list.index(min(list)) respectively.

Program

# declare a list of Integers
list = [10, 1, 2, 20, 3, 20]

# min element's position/index
min = list.index (min(list))
# max element's position/index
max = list.index (max(list))

# printing the position/index of 
# min and max elements
print("position of minimum element: ", min)
print("position of maximum element: ", max)

Output

position of minimum element:  1
position of maximum element:  3

Code Explanation

  • The minimum number of the list is 1 and it is at 1st position in the list. To get it’s index, we use list.index(min(list)) statement, min(list) returns 1 (as minimum element) and list.index(1) returns the index of 1 from the list. Hence, the position of minimum element is: 1
  • The maximum number of the list if 20 and it is two times in the list, first occurrence of 20 is at 3rd position and the second occurrence of 20 is at 5th position. Statement max(list) returns the maximum element of the list, which is 20 and the statement list.index(20) returns the index/position of first matched element. Hence, the position of maximum element is: 3

Python List Programs »

Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.