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

PythonServer Side ProgrammingProgramming

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

example

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

Output

This will give the output −

-1

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, -4, -1, -4]
m = min(my_list)
print([i for i, j in enumerate(my_list) if j == m])

Output

This will give the output −

[3, 5]
raja
Published on 05-Jan-2018 14:10:15
Advertisements