Interview Kickstart has enabled over 21000 engineers to uplevel.
Python supports object-oriented programming and has a concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. An integral part of Python are its built-in functions.
We've written a series of articles to help you learn and brush up on the most useful Python functions. In this article, we’ll learn about Python's min() function and how to use it.
If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Python Exit commands, and Type and Isinstance In Python for more content on Python coding interview preparation.
Having trained over 10,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!
At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.
Want to nail your next tech interview? Sign up for our FREE Webinar.
In this article, we’ll cover:
In Python, the min() function returns the minimum value in an iterable or out of two or more given values. It can be used in two forms: with objects or with iterables. Unlike min() in C/C++, in Python, min() can take in any type of objects and return the largest object.
The function that will form the basis for our minimum value calculation can also be specified. For example, we can evaluate the min value of strings based on lexicography, string length, etc. For strings, it by default returns the lexicographically smallest character.
However, we can specify the basis of min calculation to be another function like “len” for string length. Finally, when we pass an iterable to min() in Python, it returns the smallest item in the iterable.
Let us look at the syntax for the Python min() function when used with objects and when used with iterables:
min(item1, item2,...itemN, key = functionName)
Parameters :
Return Value:
min(iterable(s), key = functionName)
Parameters :
Return Value:
Here, we take a look at how to use the min() function in Python in the context of the data structure list, dictionary, string, or integer next time you need it:
# Usage of min() in Python
# Example of integers
intValue1 = 20
intValue2 = 60
intValue3 = 40
intValue4 = 100
# Getting the minimum value out of all four values
minValue = min(intValue1, intValue2, intValue3, intValue4)
print("The integer of the minimum value: ")
print(minValue)
# Example of strings
stringValue1 = "we"
stringValue2 = "love"
stringValue3 = "interview"
stringValue4 = "kickstart"
# Getting the lexicographic smallest string
minValue = min(stringValue1, stringValue2, stringValue3, stringValue4)
print("The lexicographically smallest string: ")
print(minValue)
# Getting the string with the minimum length (basis of comparison value comes from function len)
minValue = min(stringValue1, stringValue2, stringValue3, stringValue4, key=len)
print("The string with the minimum length: ")
print(minValue)
# Getting the lexicographically minimum character in a string
stringExample = "interviewkickstart"
print("The lexicographically minimum character in interviewkickstart: ")
minValue = min(stringExample)
print(minValue)
# Example of list
# String list
stringListExample = ["we", "love", "interview", "kickstart"]
minValue = min(stringListExample)
print("The lexicographically smallest string in the list: ")
print(minValue)
minValue = min(stringListExample, key=len)
print("The minimum length string in the list: ")
print(minValue)
# Int List
intListExample = [20, 60, 40, 100]
minValue = min(intListExample)
print("The minimum value of an integer in the list:")
print(minValue)
# Finding the index of the minimum value in the int list
indexminValue = intListExample.index(min(intListExample))
print("The minimum value", minValue, " is at position ", indexminValue + 1)
# Example of dictionary, also showing that if the container is empty, the min value will show default value
emptyDictExample = {}
minValue = min(emptyDictExample,
default={1: "Yes!"})
print("Doing min() on an empty dictionary with a given default value gives default value as min value: ")
print(minValue)
# Example of using multiple iterables of type int in min()
intListExample1 = [10, 100, 1000, 10000]
intListExample2 = [200, 600, 400, 800]
minValue = min(intListExample1,intListExample2)
print("Passing multiple iterables to min() returns the iterable with the minimum first element: ")
print(minValue)
# Example of using multiple iterables of type string in min(). Also using the default key and then len() as the key in the example
stringValueA = "we"
stringValueB = "love"
minValue = min(stringValueA, stringValueB)
print("The min value when both strings are passed with a default key (the lexicographically smallest first character): ")
print(minValue)
minValue = min(stringValueA, stringValueB, key=len)
print("The min value when both strings are passed with the length function as key: ")
print(minValue)
The integer of the minimum value:
20
The string with the first smallest lexicographic character:
interview
The string with the minimum length:
we
The lexicographically minimum character in interviewkickstart:
a
The smallest lexicographic character of the string in the list:
interview
The minimum length string in the list:
we
The minimum value of an integer in the list:
20
The minimum value 20 is at position 1
Doing min() on an empty dictionary with a given default value gives default value as min value:
{1: 'Yes!'}
Passing multiple iterables to min() returns the iterable with the minimum first element:
[10, 100, 1000, 10000]
The min value when both strings are passed with a default key (the lexicographically smallest first character):
love
The min value when both strings are passed with the length function as key:
we
Note: If the objects you pass as arguments (or the items in the iterable) are of different types, you’ll receive a TypeError. (For example, min(integer, string), where integer and string are values of two different types, will result in the output: TypeError: '>' not supported between instances of 'str' and 'int')
Found this article helpful? You can learn about more Python functions in our learn folder.
Q1. How do you find the minimum element in a container in Python?
You can find the smallest element in a container in Python by using the min() function.
Q2. What is the use of the max() and min() functions in Python?
We use Python's max() and min() functions to get the maximum or minimum object out of a set of objects or max/min object out of objects in a given iterable.
Q3. What is the difference between the Python max() and min() functions?
Both compare items or items within an iterable on some basis and return a value. But the max() function in Python returns the max value after those comparisons, and the min() function returns the minimum value.
Q4. Is min() an in-built function in Python?
Yes, min() is an in-built function in Python.
Q5. What value does the Python min() function return?
The Python min() function returns the object with the minimum value for the function that’s the basis of evaluation.
Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
Attend our webinar on
"How to nail your next tech interview" and learn