In Python, there are two powerful functions that allow you to easily find the smallest and largest values in a given dataset or collection. These functions are min() and max(). They can be applied to various data types, including numbers, strings, and more complex objects. In this article, we will explore how to use these functions effectively, along with their different variations and applications.
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 & Max in a List Using Built-in Functions
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
You can read Python List index() to learn how to find index of an item in list.
Finding the Maximum and Minimum Float Values
The min() and max() functions can also handle floating-point numbers. Consider the following list of float values:
float_numbers = [3.14, 2.718, 1.618, 0.577, 2.236]
To find the largest float value, you can call max() with the float_numbers list:
max_float = max(float_numbers) # 3.14
Similarly, to find the smallest float value, you can call min() with the float_numbers list:
min_float = min(float_numbers) # 0.577
In this case, max() returns 3.14, which is the largest float in the list, and min() returns 0.577, which is the smallest float.
We have explored various methods to delete files and directories in Python. We have learned how to delete individual files using the os.remove() function, handle errors using os.path.exists(), and delete empty directories with os.rmdir().
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
Also, check out this problem: Difference between Two Lists in Python.
Conclusion
The min() and max() functions in Python provide a convenient way to find the smallest and largest values in a given dataset or collection. Whether you're working with numbers, strings, or more complex objects, these functions offer flexibility and versatility. By customizing the comparison criteria and handling edge cases, you can unlock the full potential of these functions in your programming tasks.