Home » Python programs

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

Here, we are going to learn how to find and print the position/index of minimum and maximum elements of a list? To find the minimum and maximum elements, we use min() and max() methods in Python.
Submitted by IncludeHelp, on July 25, 2018

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

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.