
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the element from a Python list with a minimum value?
A list is one of the most commonly used data structures in Python. It is mutable and has an ordered sequence of elements. In this article, we will find the element from a given list with the minimum value.
We will see different examples and different ways to do this. But first, let us see how do define a list in Python.
Defining a List
Following is a list of integer values -
lis= [12,22,32,54,15] print(lis)
When you run the program, it will show this output -
[12, 22, 32, 54, 15]
In this article, we will look at different ways to find the minimum of a 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))
After running the program, you will get this result -
Smallest element of the list is: 0
If you also want the indices and all the places where the max element occurred, you can use the enumerate method. The enumerate method makes tuples of objects with indices at the first index and objects at the second.
Example
Following is an example to find the indices of the element from a Python list with the 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])
This output will be displayed when the program runs -
[3, 5]
Using the sort() method
Here we will find the minimum element of a list using the sort() method. The Python sort() method is used to sort the elements of the list in ascending or descending order.
By 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 the sort() method.
list1 = [100, 1, 5, 80, 20] list1.sort() print("Smallest element of the list is:", list1[0])
You will see this result after executing the program -
Smallest element of the list is: 1
Using a For Loop
We can also find the smallest element in a list, without using any built-in functions. We can use a loop to find the smallest element in a list. To do so, initially, we will assume the first element as the minimum, and then we iterate over a for loop where we compare it with other elements in the list.
While comparing with elements in the list, we change the min variable if the element is less 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))
The program gives this output when it is run -
Smallest element of the list is: 1