Python min function – Python : min() Function Tutorial with Examples

Python min function: We’ll go through the details of Python’s min() function with examples in this article.

The min() function in Python is used to find the smallest element in a collection of elements.

Python min() function

1)min() function

min() python: The min() function returns the lowest-valued object, or the lowest-valued item in an iterable.

If the values are strings, they are compared alphabetically.

Syntax:

min(iterable, *[, key, default])
min(arg1, arg2, *args[, key])

Parameters:

Iterable : An Iterable object is one that can be traversed, such as a list or a tuple.
arg1,arg2 ...... :  Multiple elements are essential.
key  :  A function that is applied to and object in the Iterable and returns a value based on the argument passed in.

Return:

It returns the Iterable or assigned elements element with the lowest value. If the main function is not available,
 the minimum value is determined by comparing the given objects. If a key function is given, instead of explicitly
 comparing objects, it will first call the key function on each one before comparing it to others.

Exceptions:

When two forms are compared, TypeError is returned.

2)Finding min value in list

Consider the following scenario: we have a list of numbers.

Since the list is an Iterable, we may directly transfer it to the min() function to find the list’s minimum value.

Below is the implementation:

# given list
givenlist = [98, 234, 21, 45, 55, 12, 988]
# finding min value in list using min function
minvalue = min(givenlist)
# print the min value
print(minvalue)

Output:

12

3)Finding min ascii character in string using min() function

Python minimum function: Assume we have a string.
Since String is an Iterable, we can use the min() function to find the character with the lowest ASCII value in the string.

Below is the implementation:

# given string
string = "onlinebtechgeeks"
# finding min ascii value character in given string
minchar = min(string)
# print the min ascii character
print(minchar)

Output:

b

The min() function compared the ASCII values of the characters in the string and returned the character with the smallest ASCII value.

4)Finding min string from list of strings using min() function

Assume we have a list of strings.
Since list is an Iterable, we may directly transfer it to the min() function to find the minimum string based on alphabetical order in the list.

Below is the implementation:

# given list of strings
strings_list = ["hello", "this", "is", "BTechGeeks"]
# finding min string  in given list of strings
minstring = min(strings_list)
# print the min string
print(minstring)

Output:

BTechGeeks

5)Finding min value and key in dictionary

We can get min value in dictionary by using lambda function as given below.

Below is the implementation:

# given dictionary
dictionary = {'Hello': 238, 'This': 135,
              'is': 343, 'BTechGeeks': 50, 'Platform': 688}
# fet the min value and key in dictionary using lambda and min () function
min_Value = min(dictionary.items(), key=lambda x: x[1])
print('Min value in dictionary : ', min_Value)

Output:

Min value in dictionary :  ('BTechGeeks', 50)

6)min() function with multiple arguments

We may also transfer individual elements to the min function rather than any Iterable.

Below is the implementation:

# min() function with multiple arguments
minvalue = min(100, 24, 454, 22, 989, 12, 5, 467)
# print the min value
print(minvalue)

Output:

5

Related Programs: