Min In Python



  1. Key In Min Python
  2. Men In Python Boots
Python

Convert seconds to hours, minutes, and seconds. In Python, the date and time module provides various functions for the manipulation of dates. We can also convert seconds into hours, minutes, and seconds by applying mathematical operations. Let us discuss the various ways to perform the conversion. Python min and max are built-in functions in python which returns the smallest number and the largest number of the list respectively, as the output. Python min can also be used to find the smaller one in the comparison of two variables or lists.

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,

  • 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.
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.
Output:
Find the character with min ASCII value in a String using min()

Suppose we have a string i.e.
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.
Output:
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.
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.
Output:

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.
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.
Output:

Key In Min Python

Find item in a dictionary with minimum value using min()


Output:
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

Men In Python Boots

We can also pass the individual elements in min function instead of any Iterable i.e.
Output:
Complete example is as follows,
Output:

Min In Python

Related Posts: