min() Function in Python

min() Function in Python

4 mins read305 Views Comment
Updated on Apr 11, 2023 23:30 IST

This article is talking about min() Function.The concept is explained with programming examples.

2023_03_MicrosoftTeams-image-355.jpg

The min() function is an in-built technique to determine the lowest value within a list. Alternatively, when given a list of strings, it returns the string with the smallest lexicographic value.

In this article, we will discuss the min() function and understand its use with the help of examples. We will be covering the following sections:

Introduction min() Function in Python

In Python, the built-in min() function finds the smallest element or value in a given iterable such as a list, tuple, set, or even a string. 

The function takes one or more arguments as input and returns the minimum value among them. It can also accept an optional argument key, which specifies a function of one argument to extract a comparison key from each element in the iterable. If the iterable is empty, the function raises a ValueError

Syntax of min()

The syntax of the min() function in Python is given as follows:

min(iterable, *[, key, default])

Parameters of min()

Here,

·     iterable: This is the required iterable or sequence, such as a list, tuple, set, or string, from which the minimum value is to be retrieved.

·     *: This indicates that the key and default arguments are optional.

·     key: This is an optional argument that specifies a function of one argument to extract a comparison key from each element in the iterable.

·     default: This is an optional argument that specifies the value to be returned if the iterable is empty. If it is not provided, the function raises a ValueError.

Note that the min() function can take multiple arguments, but the first argument must always be an iterable or a sequence.

Return Value of min()

This function returns:

· the minimum element in the case of integers or floating type values. 

· the lexicographically smallest character in the string.

Exceptions of min()

If a list that contains elements of various data types is passed as a parameter to the min() function, it will raise a TypeError Exception.

Top 10 Python Built in Functions for Data Science
For Loop in Python (Practice Problem) – Python Tutorial
Working with Python Numbers
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Examples of Using the min() Function in Python

Example 1: Find the minimum value in a list of integers

numbers = [4, 8, 1, 9, 3, 5]

min_value = min(numbers)

print(min_value)

Output:

1

In this example, we have a list of integers called numbers. We pass this list as an argument to the min() function, which returns the smallest value in the list, i.e., 1. We store this value in the min_value variable and print it.

Example 2: Find the minimum value in a tuple of floating-point numbers

values = (3.5, 2.8, 4.2, 1.6, 5.1)

min_value = min(values)

print(min_value)

Output:

1.6

In this example, we have a tuple of floating-point numbers called values. We pass this tuple as an argument to the min() function, which returns the smallest value in the tuple, i.e., 1.6. We store this value in the min_value variable and print it.

Example 3: Find the minimum value in a set of integers

numbers = {2, 7, 1, 9, 4}

min_value = min(numbers)

print(min_value)

Output:

1

In this example, we have a set of integers called numbers. We pass this set as an argument to the min() function, which returns the smallest value in the set, i.e., 1. We store this value in the min_value variable and print it.

Example 4: Find the smallest lexicographic string in a list of strings

fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’]

min_fruit = min(fruits)

print(min_fruit)

Output:

apple

In this example, we have a list of strings called fruits. We pass this list as an argument to the min() function, which returns the smallest lexicographic string in the list, i.e., ‘apple’. We store this value in the min_fruit variable and print it.

Example 5: Case of TypeError Exception

list=[1,2,”a”]

 print(min(list))

Output:

TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’

In this example, we have passed a list containing both integer and String elements. This will cause TypeError because the min() method only compares the elements of the same type.

Example 6: Find the minimum value in a list of dictionaries based on a specific key

students = [{‘name’: ‘Alice’, ‘age’: 19}, {‘name’: ‘Bob’, ‘age’: 20}, {‘name’: ‘Charlie’, ‘age’: 18}]

min_student = min(students, key=lambda s: s[‘age’])

print(min_student)

Output:

{‘name’: ‘Charlie’, ‘age’: 18}

In this example, we have a list of dictionaries called students, where each dictionary represents a student with a name and an age. We pass this list as an argument to the min() function and use the key parameter to specify a lambda function that extracts the age value from each dictionary. The min() function returns the dictionary with the smallest age value, i.e., {‘name’: ‘Charlie’, ‘age’: 18}. We store this value in the min_student variable and print it.

Endnotes

In conclusion, the min() function can be useful in various applications, such as data analysis, mathematical operations, and sorting algorithms.

This article was helpful for you in understanding how and why the min() method is used in Python. You can explore related articles here if you wish to learn more about Python and practice Python programming.

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio