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



List is one of the most commonly used data structures provided by python. List is a data structure in python that is mutable and has an ordered sequence of elements. Following is a list of integer values −

Example

Following is a list of integer values.

lis= [12,22,32,54,15] print(lis)

Output

If you execute the above snippet, it produces the following output.

[12, 22, 32, 54, 15]

In this article, we will look at different ways to find the minimum of the list in Python. For the above list, the minimum element of the list is 1.

Using the min() method

In this method, we use the min() method to find the minimum valued element. The min() method returns an element from the list with the minimum value.

Example

The following is an example code for finding the minimum element of a list using the min() method.

list1 = [1, 0, 4, 5, 100] print("Smallest element of the list is:", min(list1))

Output

If you execute the above snippet, it produces the following output.

Smallest element of the list is: 0

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

Following is an example to find the indices of the element from a python list with minimum value −

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]

Using the sort() method

Here we will find the minimum element of a list using the sort() method. The sort() method which is provided by python is used to sort the elements of the list in ascending or descending order. Default, it will sort in ascending order. After sorting the smallest element is at the first position of the list, so we print that element.

Example

The following example code finds the smallest element of the list using sort() method.

list1 = [100, 1, 5, 80, 20] list1.sort() print("Smallest element of the list is:", list1[0])

Output

If you execute the above snippet, it produces the following output.

Smallest element of the list is: 1

Using for loop or without using the built in function.

In this method, we will not use built in functions rather we use a loop to find the smallest element in a list.

Here initially assume the first element as the minimum and then we iterate over a for loop where we compare with other elements in the list. While comparing with elements in the list we change the min variable if the element is lesser than the compared minimum element. Finally, after getting terminated from the loop we get the minimum element.

Example

The following is an example code to get the minimum element from a list.

def MinElement(lst): min = lst[0] for ele in lst: if ele < min : min = ele return min lst = [100, 1, 5, 80, 20] print("Smallest element of the list is:", MinElement(lst))

Output

If you execute the above snippet, it produces the following output.

Smallest element of the list is: 1

Advertisements