In the previous article, we have discussed Python Program to Find Vertex, Focus and Directrix of Parabola
max() function :
max() is a built-in function that returns the maximum value in a list.
index() function:
This function searches the lists. It returns the index where the value is found when we pass it as an argument that matches the value in the list. If no value is found, Value Error is returned.
Given a list, the task is to Get the position of Max Value in a List.
Examples:
Example 1 :
Input :
Given List = [1, 5, 9, 2, 7, 3, 8]
Output:
Maximum Value in the above Given list = 9 Position of Maximum value of the above Given List = 3
Example 2 :
Input :Â
Given List = [4, 3, 7, 1, 2, 8, 9]
Output:
Maximum Value in the above Given list = 9 Position of Maximum value of the above Given List = 7
Program to Get the Position of Max Value in a List
Below are the ways to Get the Position of Max value in a List.
Method #1: Using Max(),Index() functions (Static Input)
Approach:
- Give the List as static input and store it in a variable.
- Get the maximum value of the given list using the built-in max() function and store it in another variable.
- Print the maximum value of the above-given List.
- Get the position of the maximum value of the given List using the built-in index() function and store it in another variable.
- Print the position of the maximum value of the given List i.e. maximum position+1( since list index starts from zero).
- The Exit of the program.
Below is the implementation:
# Give the List as static input and store it in a variable. Gvn_lst = [1, 5, 9, 2, 7, 3, 8] # Get the maximum value of the given list using the built-in max() function and # store it in another variable maxim_vle = max(Gvn_lst) # Print the maximum value of the above given List. print("Maximum Value in the above Given list = ", maxim_vle) # Get the position of the maximum value of the List using the built-in index() function # and store it in another variable. maxim_positn = Gvn_lst.index(maxim_vle) # Print the position of the maximum value of the given List i.e. maximum position+1 # ( since list index starts from zero). print("Position of Maximum value of the above Given List = ", maxim_positn+1)
Output:
Maximum Value in the above Given list = 9 Position of Maximum value of the above Given List = 3
Method #2: Using Max(),Index() functions (User Input)
Approach:
- Give the list as User input using list(),map(),input(),and split() functions and store it in a variable.
- Get the maximum value of the given list using the built-in max() function and store it in another variable.
- Print the maximum value of the above-given List.
- Get the position of the maximum value of the given List using the built-in index() function and store it in another variable.
- Print the position of the maximum value of the given List i.e. maximum position+1( since list index starts from zero).
- The Exit of the program.
Below is the implementation:
#Give the list as User input using list(),map(),input(),and split() functions and store it in a variable. Gvn_lst = list(map(int, input('Enter some random List Elements separated by spaces = ').split())) # Get the maximum value of the given list using the built-in max() function and # store it in another variable maxim_vle = max(Gvn_lst) # Print the maximum value of the above given List. print("Maximum Value in the above Given list = ", maxim_vle) # Get the position of the maximum value of the List using the built-in index() function # and store it in another variable. maxim_positn = Gvn_lst.index(maxim_vle) # Print the position of the maximum value of the given List i.e. maximum position+1 # ( since list index starts from zero). print("Position of Maximum value of the above Given List = ", maxim_positn+1)
Output:
Enter some random List Elements separated by spaces = 4 3 7 1 2 8 9 Maximum Value in the above Given list = 9 Position of Maximum value of the above Given List = 7
Here we printed the index of the maximum element of the given list.
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.