Min() Function in Python

In this Python tutorial, We will see what is the min() function in Python and how we can apply this min() function to different data types in Python with demonstrative examples.

What is the min() function in Python

Python’s min() function is a built-in function used to return the smallest item from an iterable (like a list, tuple, set, or string) or two or more arguments.

In its simplest form, this could be the smallest among the numbers, the smallest string in lexicographical order, or the smallest item in an iterable such as a Python list or a tuple. If multiple items are minimum and occur more than once, the function will return the first one encountered.

Syntax and Parameters

Here’s the basic syntax of the min() function in Python:

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

Or

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

Parameters:

  • iterable: An iterable like list, tuple, string, etc., from which the smallest item is to be found.
  • *arg1, arg2, args: Two or more values to compare and find the smallest.
  • key (optional): A function to serve as a key or a basis of sort comparison.
  • default (optional): A value to return if the provided iterable is empty.

Application of the min() function in Python

The min() function can be applied to many Data types in Python. They can be:

  • Numbers
  • Strings
  • Lists
  • Tuples
  • Sets
  • Dictionaries

We will see how the min() function works with all of these data types in Python one by one, with examples.

Application of the min() function in Python on numbers.

The most straightforward use of Python’s min() function is finding the smallest number among a series of numbers.

Let’s just take some random numbers (Integers or Float), and try to find the minimum value out of them in Python:

print(min(11, 22.5, 55, 88.3, 42, 64))

The output will be:

11.0
The min() function in Python on random numbers

Or, we can save these numbers in some variables and then compare those variables with the min() function in Python,

Let’s consider a scenario where we have average temperatures (in Fahrenheit) of five major cities in the USA in Python, and we want to find out which city has the lowest average temperature.

# Average Temperatures of major cities in Fahrenheit
new_york = 52.5
los_angeles = 63
chicago = 49.5
houston = 70
san_francisco = 57

lowest_temperature = min(new_york, los_angeles, chicago, houston, san_francisco)

print('The lowest average temp.(in F) recorded is:', lowest_temperature)

The output is:

The lowest average temp.(in F) recorded is: 49.5

In this Python code, we are using the min() function to compare the average temperatures of five major cities: New York, Los Angeles, Chicago, Houston, and San Francisco. The min() function in Python then returns the lowest value among these temperatures, which is 49.5, the average temperature of Chicago.

The min() function in Python on numbers

This way we can apply the min() function to the numbers in Python.

Application of the min() function on strings in Python

We can use the min() function with strings too in Python. The function then determines the smallest string based on lexicographical order (basically, dictionary order).

Let’s consider we have names of a few US states saved as strings in Python, and we want to find out which state would come first if arranged in dictionary order, also known as lexicographical order.

first_state = min("Texas", "Florida", "California", "Arizona", "New York", "Alabama")

print('The first state, if we arrange this in dictionary order is :', first_state)

The output is:

The first state, if we arrange this in dictionary order is : Alabama
The min() function in Python on string

Note: Like Numbers, we can store the strings also in the variable and can compare them.

This way we can apply the min() function on strings in Python to find the minimum value between them.

Application of the min() function on Python Lists

We can use the min() function on Python lists in Various ways:

Case 1: In this example, we have a Python list with random numbers and we will try to find the minimum value within that number stored inside the Python list using the min() function.

Note: We can do this with a Python list containing strings too.

Numbers_list = [11.5, 22, 545, 2, 402, 58, 6.91]

print(min(Numbers_list))

The Output is:

2
The min() function in Python on a list with numbers

Case 2: Nested lists, or lists containing other lists, are common data structures. We can also compare numbers or strings stored within a nested Python list using the min() function.

Let’s say we have the average monthly rainfall (in inches) for two cities, New York and Los Angeles, for the first quarter of the year. Here is how we can find the month with the minimum rainfall:

In this example, we use the Python min() function twice. The inner Python min() function is applied to the ‘rainfall’ Python list, and it returns a list of the smallest elements from each inner list, effectively giving us the smallest rainfall for each city. Then, the outer Python min() function finds the smallest among these values.

# Average rainfall in inches
# [New York], [Los Angeles] respectively

rainfall = [[3.2, 3.1, 4.4], [2.8, 2.6, 2.3]]

lowest_rainfall = min(min(rainfall))

print(lowest_rainfall)

The output is:

2.3
The min() function in Python on a nested list

This way we can apply the min() function with lists in Python.

Application of the min() function on tuples in Python

Same like the Python list, we can easily apply the min() on tuples in Python.

Suppose we have a tuple containing the populations (in millions) of the five most populous cities in the USA. We want to find out which city has the smallest population among these cities.

In this example, we have a Python tuple of the populations of the five most populous cities in the USA. The populations are in the same order as the cities they represent (i.e., New York, Los Angeles, Chicago, Houston, and Phoenix).

When we pass this tuple to the min() function in Python, it goes through all the elements of the tuple and returns the smallest value it finds. In this case, the smallest population is 1.7 million, which belongs to Phoenix.

# Populations in millions
# New York, Los Angeles, Chicago, Houston, Phoenix respectively
city_populations = (8.4, 4.0, 2.7, 2.3, 1.7)

smallest_population = min(city_populations)

print('The smallest population(in millions) of the city is: ', smallest_population)

The output is:

The smallest population(in millions) of the city is:  1.7
The min() function in Python on a tuples

This way we can apply the min function in Python to find the minimum value stored in a tuple.

Application of the min() function in Python on sets

The min() function can also be used on sets in Python. A set in Python is an unordered collection of unique elements. Like lists and tuples, we can find the minimum value in a set using the min() function.

let’s consider a Python set that represents the finish times (in minutes) of the top five runners in the Boston Marathon:

In this example, the set finish_times contains the finish times for the top five runners in the Boston Marathon. When we pass this set to the min() function in Python, it iterates over all the elements in the set and returns the smallest value.

# Finish times in minutes
# Times for runners 1 to 5 respectively
finish_times = {125, 128, 127, 130, 132}

fastest_time = min(finish_times)

print('The fastest timing is:', fastest_time)

The Output is: In this case 125 is the smallest value. This corresponds to the fastest finish time among the top five runners.

The fastest timing is: 125
The min() function in Python on a sets

This way we can apply the min() function on sets in Python to find the minimum values stored in it.

Application of the min() function on dictionaries in Python

We can also apply the min() function on a Python dictionary. Let’s check what happens when we apply:

For instance, Suppose we have a dictionary in Python with the names of five US states as keys and their populations in millions as values. We want to find out which state among these has the smallest population.

When we pass this Python dictionary to the min() function, it would normally return the key with the smallest value in lexicographical order. However, we’re interested in the state with the smallest population, not the smallest key.

To achieve this, we use the key parameter of the min() function, specifying state_populations.get. This makes the min() function in Python operate on the dictionary’s values rather than its keys.

The min() function then goes through all the values of the Python dictionary and returns the key of the smallest value it finds.

# Populations in millions
state_populations = {
    "California": 39.51,
    "Texas": 28.99,
    "Florida": 21.48,
    "New York": 19.45,
    "Pennsylvania": 12.80
}

smallest_population_state = min(state_populations, key=state_populations.get)

print(smallest_population_state)

The Output is: In this case, ‘Pennsylvania’ has the smallest population of 12.80 million among the given states.

Pennsylvania
The min() function in Python on a dictionary

This way we can apply the min() function to a dictionary in Python.

Conclusion

Python’s built-in min() function is an incredibly powerful and versatile tool that can help find the smallest item in various types of iterable objects including lists, tuples, sets, and dictionaries. It can even be extended to find minimums in more complex structures such as nested lists.

You may also like to read: