Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Minimum Function in Python

Last Updated on June 30, 2023 by Mayank Dham

The min function or minimum function in Python is a built-in function that allows you to find the smallest value among a collection of elements. Whether you’re dealing with lists, tuples, or other iterable objects, the min function provides a convenient and efficient way to retrieve the minimum element. The min function in Python can accept multiple iterables as arguments, allowing you to compare elements across different collections simultaneously. For example, you can find the minimum value among two lists or a list and a tuple.

What is the Minimum Function in Python?

The min function in Python is a built-in function that is used to find the smallest value among a collection of elements. It takes one or more arguments, which can be iterable objects such as lists, tuples, sets, or even strings, and returns the smallest element.

The min function works by comparing each value in the iterable and returning the smallest value. The minimum function in python can be used with any iterable, whether it contains numbers, strings, or other data types.

Syntax of the min Function

*min(iterable, iterables, key=default, default=object(), kwargs)

Parameter of min Function:

iterable: This is the required parameter that represents the collection of elements from which you want to find the minimum value.

*iterables (optional): Additional iterables can be passed to compare elements across multiple collections.

key (optional): It allows you to specify a function that will be applied to each element before comparison. This function defines the criteria by which the minimum value is determined.

default (optional): If the iterable is empty and no default value is provided, the min function will raise a ValueError. However, you can specify a default value to return in such cases.

**kwargs (optional): Additional keyword arguments can be passed, but they are mainly used for compatibility purposes.

Example
In this example we have use min function to get the minimum element from collections

Code Implementation:

x=min(1,2,3,4,5)
print(x)

Output

1

Explanation
In this above python program we make a variable x which store the minimum value from the collections by using minimum function So as we know out of these number 1 is smallest so we got 1 on output screen.

Return Value of the Min Function in Python

The return value of the minimum function in Python is the minimum value from the iterable. The data type of the return value will depend on the data type of the iterable. For example, if the iterable contains integers, the return value will be an integer. If the iterable contains strings, the return value will be a string.

If the iterable is empty and a default value is provided, the default value will be returned. If the iterable is empty and a default value is not provided, the min function will raise a ValueError.

Parameters of the Min Function in Python

The min function in Python can take two optional parameters:

  • key: A function that is used to determine the value to be compared. If this argument is not provided, the min function will compare the values directly.
  • default: A value that is returned if the iterable is empty. If this argument is not provided, the min function will raise a ValueError.
  • Iterable: An iterable object, such as a string, list, or tuple. Instead of using an iterable, we can pass elements directly to the min() function.

The key parameter is used to specify a function that is applied to each element in the iterable before comparison. The key function should take one argument and return a value that will be used for comparison. The default value of the key parameter is None, which means that the values in the iterable will be compared directly.

The default parameter is used to specify a value that is returned if the iterable is empty. If the default parameter is not provided, the min function will raise a ValueError.

Examples of Min Function in Python

In this section, we will explore some examples of using the "min" function in Python.

Example 1: Using the "min" function with a list of integers
Here we use min function to find minimum number in the list.

# your code goes here

numbers = [1, 5, 3, 9, 2]
smallest_number = min(numbers)
print(smallest_number)

Output:

1

In this example, we have a list of integers called "numbers." We use the "min" function to find the smallest number in the list and store it in the "smallest_number" variable. We then print the value of the "smallest_number" variable, which is 1.

Example 2: Using the "min" function with a list of strings
Here we use min function to find minimum string from the list

fruits = ["apple", "banana", "orange", "kiwi", "grape"]
smallest_fruit = min(fruits)
print(smallest_fruit)

Output:

 apple

In this example, we have a list of strings called "fruits." We use the "min" function to find the smallest string in the list and store it in the "smallest_fruit" variable. We then print the value of the "smallest_fruit" variable, which is "apple."

Example 3 Finding the minimum value in a dictionary based on the values:
The min() function can also be used to find the minimum value in a dictionary based on the values. Here’s an example:

prices = {'apple': 0.75, 'banana': 0.50, 'orange': 1.00}
cheapest_fruit_price = min(prices.values())
print(cheapest_fruit_price)

Output:

0.50

In this example, we have a dictionary of fruit prices, where the keys are the fruit names and the values are the prices. We use the min() function to find the minimum value among the dictionary values, which gives us the cheapest fruit price.

Example 4 Using a key function to find the minimum value:
Sometimes we want to find the minimum value based on some criteria other than the default ordering of the values. We can do this by using the key argument of the min() function. Here’s an example:

# your code goes here

words = ['apple', 'banana', 'orange', 'kiwi']
shortest_word = min(words, key=len)
print(shortest_word)

Output:

kiwi

In this example, we have a list of words, and we want to find the shortest word in the list. We pass the len function as the key argument to the min() function, which tells the function to use the length of each word as the criterion for finding the minimum value. The function returns the shortest word, which is ‘kiwi’

Conclusion
In conclusion, the min function in Python is a versatile and powerful tool that allows you to easily find the minimum value among a collection of elements. Whether you’re working with lists, tuples, sets, or even strings, the min function provides a convenient and efficient solution for identifying the smallest element.
By understanding the syntax and parameters of the min function, you can leverage its flexibility to handle various scenarios. The optional key parameter enables you to customize the comparison criteria, allowing you to find the minimum value based on specific conditions or transformations of the elements.

Frequently Asked Questions (FAQ) on min function in python

Q1: What happens if the iterable passed to the min function is empty?
A1: By default, if the iterable is empty, the min function raises a ValueError. However, you can provide a default value using the default parameter to avoid the error and return the specified default value instead.

Q2: Can the min function compare elements of different types?
A2: Yes, the min function can compare elements of different types as long as they are comparable. For example, you can find the minimum value among integers, floats, strings, dates, or even custom objects, as long as the comparison is defined for those types.

Q3: How can I find the minimum value based on a specific property of objects in a list?
A3: You can use the key parameter to specify a function that extracts the desired property from each object before comparison. For example, if you have a list of objects with a ‘price’ attribute, you can use min(objects, key=lambda obj: obj.price) to find the object with the minimum price.

Q4: Is it possible to find multiple minimum values using the min function?
A4: No, the min function returns only the first occurrence of the minimum value. However, you can use other techniques like list comprehension or the filter function to retrieve all the minimum values from the collection.

Q5: Can I use the min function on strings to find the shortest string?
A5: Yes, you can use the min function on strings to find the shortest string based on their lengths. You can specify key=len as the key parameter to compare the strings based on their lengths and retrieve the shortest one.

Leave a Reply

Your email address will not be published. Required fields are marked *