Here, we will figure out how to discover and print the position/file of least and most extreme components of a list? To locate the base and greatest components, we use min() and max() techniques in Python.
Given a list and we need to discover the record/position of least and greatest components of a list in Python.
Example:
Input:
list = [10, 1, 2, 20, 3, 20]
Output:
Positive of minimum element: 1
Positive of maximum element: 3
Rationale:
To discover the positions/lists of least and most extreme components of a list, we have to locate the greatest and least components of the list – to locate the most extreme component of the list, we will utilize max(list) and to locate the base component of the list, we will utilize min(list).
Furthermore, to get their records, we will utilize list.index(max(list)) and list.index(min(list)).
Program to discover the situation of min and max components 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
Clarification:
- The base number of the list is 1 and it is at first position in the list. To get its a list, we use list.index(min(list)) articulation, min(list) returns 1 (as least component) and list.index(1) restores the list of 1 from the list. Thus, the situation of the least component is: 1.
- The most extreme number of the list if 20 and it is multiple times in the list, the first event of 20 is at third position and the second event of 20 is at fifth position. Explanation max(list) restores the most extreme component of the list, which is 20 and the announcement list.index(20) restores the list/position of the first coordinated component. Subsequently, the situation of the most extreme component is: 3