Get the position of max value in a list in Python
In this tutorial, we will write the program to find the position of the maximum value in a list in Python.
Before writing the code, we will discuss the argument and function which we will use in the program.
In other languages like C, we have to write big lines of code to find the position of the maximum value in the array. But in Python, we can find the position of the maximum value in one line using the index() and max().
index(): Index searches the lists. When we pass it as an argument that matches the value in the list, it returns the index where the value is found. If no value is found then its returns ValueError.
max(): It is an inbuilt function that directly returns the maximum value in lists.
min(): It is an inbuilt function that directly returns the minimum value in lists.
Program:
See our Python program below:
#pass tha value in list list = [4,7,2,6,4,0,1,9] #Lets find the maximum value in lists maxvalue = max(list) print("Maximum Value in the list is:", maxvalue) #Now we have find the position of Max Value maxpos = list.index(maxvalue) print("\nPosition of max value in the list is:", maxpos+1)
In the above program, you can understand from the comments in the code.
Output:
Maximum Value in the list is: 9 Position of max value in the list is: 8
Index() start the counting from 0, 1, 2, 3, …… That why inline 10 we wrote maxpos+1.
You can also learn:
Python program to get maximum and minimum number from a Python list
Leave a Reply