In this article we will discuss detailed features of Python’s min() function with examples.
Python provides a min() function to find out the smallest element from a collection of elements,
min(iterable, *[, key, default]) min(arg1, arg2, *args[, key])
- Arguments:
- Iterable : An Iterable object like list, tuple etc.
- arg1, arg2 … : Multiple elements
- key : A function that will be applied to each item in the Iterable and it returns a value based on the passed argument.
- Returns:
- It returns the element with minimum value in the Iterable or given elements. If key function is not provided then it directly compare the given items to find out the minimum value. If key function is provided then instead of comparing items directly it will call the key function on each item and then compare it with others.
Let’s see how to use the min() function.
Using min() function with an Iterable like list or tuple etc.
Find minimum value in a list using min()
Suppose we have a list of numbers i.e.
# List of numbers listOfNum = [22, 34, 11, 33, 10, 35, 19, 33, 9, 10]
As list is an Iterable, so to find the minimum value in the list we can directly pass it to the min() function i.e.
# Find minimum value in a list minValue = min(listOfNum) print('minimum value in List : ', minValue)
Output:
minimum value in List : 9
Find the character with min ASCII value in a String using min()
Suppose we have a string i.e.
sampleStr = 'thisissamplestring'
As String is an Iterable, so to find the character with minimum ASCII value in the string we can directly pass it to the min() function i.e.
# Find character with min ASCII value in string minValue = min(sampleStr) print('Character with min ASCII value : ', minValue)
Output:
Character with min ASCII value : a
min() function compared the characters in the string based on their ASCII value and returned the character with smallest ASCII value.
Find min string from list of Strings based on alphabetical order using min()
Suppose we have a list of strings i.e.
listOfStr = ['hi', 'this', 'is', 'a', 'small', 'string', 'with', 'msg']
As list is an Iterable, so to find the minimum string based on alphabetical order in the list we can directly pass it to the min() function i.e.
# Get string with min value in list of string based on alphabetical minValue = min(listOfStr) print(minValue)
Output:
a
Using min() function with Iterable & Custom comparater / key function
Till now we have seen examples where we are using min() function with default comparator i.e. it will use < operator to compare the elements while searching for min value. What if we want to find min element based on our custom logic or custom comparator.
Let’s see how to do that,
Find min length string from list of Strings using min()
Suppose we have a list of strings i.e.
listOfStr = ['hi', 'this', 'is', 'a', 'small', 'text', 'with', 'msg']
Now we want to find the string with minimum size in this list of string. For that we need to pass the key argument to the min function i.e.
# Get the string with minimum size in this list of string minValue = min(listOfStr, key=lambda x: len(x)) print('min Length String in List of Strings : ', minValue)
Output:
min Length String in List of Strings : a
Find item in a dictionary with minimum value using min()
sampleDict = {'Ritika': 5, 'Sam': 27, 'John': 10, 'Sachin': 14, 'Mark': 19} # Get Item with min value in dictionary minValue = min(sampleDict.items(), key=lambda x: x[1]) print('Item with min Value in Dictionary : ', minValue)
Output:
Item with min Value in Dictionary : ('Ritika', 5)
Some Important Points:
min function can find min element from similar type elements. If we pass different type elements then it will raise error min() will always will always return the first encountered minimum element. If there are multiple min elements then we need a customized solution to find all the min elements.
Using min() function with Multiple arguments
We can also pass the individual elements in min function instead of any Iterable i.e.
# Find minimum value in a given numbers minValue = min(22, 34, 11, 33, 10, 35, 19, 33, 9, 10) print('minimum value : ', minValue)
Output:
minimum value : 9
Complete example is as follows,
def main(): print('*** Find minimum value in a list using min() ***') # List of numbers listOfNum = [22, 34, 11, 33, 10, 35, 19, 33, 9, 10] print('List Contents : ', listOfNum) # Find minimum value in a list minValue = min(listOfNum) print('minimum value in List : ', minValue) print('*** Find the character with min value in a String using min() ***') sampleStr = 'this is sample string' # Find character with min ASCII value in string minValue = min(sampleStr) print('Character with min ASCII value : ', minValue) print('*** Find min string from list of Strings based on alphabetical order using min() ***') listOfStr = ['hi', 'this', 'is', 'a', 'small', 'string', 'with', 'msg'] # Get string with min value in list of string based on alphabetical minValue = min(listOfStr) print(minValue) print('*** Using min() function with Iterable & Custom comparater / key function ***') print('** Find min length string from list of Strings using min() **') listOfStr = ['hi', 'this', 'is', 'a', 'small', 'text', 'with', 'msg'] # Get the string with minimum size in this list of string minValue = min(listOfStr, key=lambda x: len(x)) print('min Length String in List of Strings : ', minValue) print('** Find item in a dictionary with minimum value using min() **') sampleDict = {'Ritika': 5, 'Sam': 27, 'John': 10, 'Sachin': 14, 'Mark': 19} # Get Item with min value in dictionary minValue = min(sampleDict.items(), key=lambda x: x[1]) print('Item with min Value in Dictionary : ', minValue) print('*** Using min() function with Multiple arguments ***') # Find minimum value in a given numbers minValue = min(22, 34, 11, 33, 10, 35, 19, 33, 9, 10) print('minimum value : ', minValue) if __name__ == '__main__': main()
Output:
*** Find minimum value in a list using min() *** List Contents : [22, 34, 11, 33, 10, 35, 19, 33, 9, 10] minimum value in List : 9 *** Find the character with min value in a String using min() *** Character with min ASCII value : a *** Find min string from list of Strings based on alphabetical order using min() *** a *** Using min() function with Iterable & Custom comparater / key function *** ** Find min length string from list of Strings using min() ** min Length String in List of Strings : a ** Find item in a dictionary with minimum value using min() ** Item with min Value in Dictionary : ('Ritika', 5) *** Using min() function with Multiple arguments *** minimum value : 9
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.