Q:

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

belongs to collection: Python List Programs

0

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

Prerequisite:

Example:

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

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

Logic:

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

And, to get their indexes, we will use list.index(max(list)) and list.index(min(list)).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find the position of min and max elements of a list in Python

# 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

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Program to input, append and print the li... >>
<< Python | Program to Print the index of first match...