Computer >> Computer tutorials >  >> Programming >> Python

How to find the element from a Python list with a maximum value?


To find the element with the maximum value, you need to call the max() function with the list as argument. The max function iterates over the list keeping track of the maximum value it encountered till it reaches the end. Then it returns this value. 

example

my_list = [2, 3, 1, 5, -1]
print(max(my_list))

Output

This will give the output −

5

If you also want the indices and all the places where max element occurred, you can use the enumerate method. The enumerate method makes tuples of objects with indices at first index and objects at second.

Example

my_list = [2, 3, 1, 5, -1, 5]
m = max(my_list)
print([i for i, j in enumerate(my_list) if j == m])

Output

This will give the output −

[3, 5]