To find the biggest and tiniest item in a list, Python gives us two easy functions to do. Today we'll look at Python's built-in min() and max() functions, with the help of some examples.
What is max() and min() in Python?
The min() and max() are built-in functions of Python programming language to find the smallest and the largest elements in any iterable. These functions come in handy when working with any iterables like lists, tuples, sets, and dictionaries in Python.
The min() function takes an iterable as an argument and returns the smallest item in the iterable. Similarly, the max() function accepts an iterable as an input and returns the iterable's largest item.
The basic syntax for both functions is 'max(iterable)' and 'min(iterable)'.
Find Min & Mix in a list
If you have a list of integers, for example, you can use max() to get the largest integer in the list and min() to find the fewest number of lists:
l=eval(input("Enter a list of numbers")) # [4,7,9,10,45,21,46,67,23] --- input print("min=",min(l)) print("max=",max(l))
Output:
min= 4 max= 67
Min & Max of the list using a loop
If you don’t wish to use the pre-defined min() and max() functions, you can get the same desired result by writing a few lines of code using a for loop and iterating over the whole list manually to find the largest and smallest value.
Here is the algorithm for this solution:
- Keep the first item on the list in a variable called large for the max value and small for the min value.
- Now, cycle through the list of elements and compare them to one another. Assign the current element to small if it is less than small. You now have the new minimum value.
- Similarly, cycle through the list elements and compare them to one another. Assign the current element to large if it is greater than large. You now have the new maximum value.
- Repeat the preceding steps until you reach the end of the list. The actual minimum and maximum value of the string will be stored in the last value of a small and large variables.
- Return large and small respectively.
Code:
l=eval(input("Enter a list of numbers")) # [4,7,9,10,45,21,46,67,23] --- input larg = l[0] # initialize with the first value small =l[0] # initialize with the first value for num in l: if num > larg: larg = num if num < small: smallest = num print("Largest:", larg) # Output: Largest: 9 print("Smallest:", small) # Output: Smallest: 1
Output:
Largest: 67 Smallest: 4
Finding Max & Min between 2 lists
If you are given two or more lists and you have to find the largest and smallest element in all of them, then we have two different ways to solve this problem.
We can use the ‘+’ operator to first combine all the given lists and then apply the max() or min() function to find the desired result.
Example:
l1=eval(input("Enter the list1 of numbers")) l2=eval(input("Enter the list2 of numbers")) l3=eval(input("Enter the list3 of numbers")) # l1=[4,6 ,9,35,12,23,10] # l2=[0,9,29,56,23,56,18] # l3=[1,2,3,4,5,6,7,8,9,10] max_all = max(l1+l2+l3) min_all = min(l1+l2+l3) print ("The maximum of all 3 lists is : " + str(max_all)) print ("The minimum of all 3 lists is : " + str(min_all))
Output:
The maximum of all 3 lists is : 56 The minimum of all 3 lists is : 0
Or we can use the chain() function, which is faster than that in computation time, to combine the lists.
Example:
from itertools import chain l1=eval(input("Enter the list1 of numbers")) l2=eval(input("Enter the list2 of numbers")) l3=eval(input("Enter the list3 of numbers")) # l1=[4,6 ,9,35,12,23,10] # l2=[0,9,29,56,23,56,18] # l3=[1,2,3,4,5,6,7,8,9,10] max_all = max(chain(l1,l2, l3)) min_all = min(chain(l1,l2,l3)) print ("The maximum of all 3 lists is : " + str(max_all)) print ("The minimum of all 3 lists is : " + str(min_all))
Output:
The maximum of all 3 lists is : 56 The minimum of all 3 lists is : 0
Conclusion
So in this article, we covered the built-in Python functions: min() and max() as well as created a new function to discover the minimum and maximum values in a list using a loop. Happy Learning :)