Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

The min() Function in Python

Last updated by Vartika Rai on Apr 01, 2024 at 01:04 PM | Reading time: 6 minutes

The fast well prepared banner

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

The min() Function in Python
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

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:

  • What Is the min() Function in Python and What Does It Do?
  • The min() Function in Python: Syntax
  • The min() Function in Python: Example
  • FAQs on the min() Function in Python

What Is the min() Function in Python and What Does It Do?

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.

The min() Function in Python: Syntax

Let us look at the syntax for the Python min() function when used with objects and when used with iterables:

The min() function with objects: Syntax


min(item1, item2,...itemN, key = functionName) 

Parameters : 

  • item1, item2,...itemN: N objects of the same datatype.
  • key: The comparison of the objects is performed based on this function’s return value.
  • functionName: Name of the key. In other words, the name of the function whose return value forms the basis of comparison.

Return Value: 

  • The object/item with the minimum value for the function that forms the basis of comparison is returned.
  • If a single string is passed as an object, the character with the minimum lexicographic value is returned.

The min() function with iterables: Syntax


min(iterable(s), key = functionName) 

Parameters : 

  • iterable(s): An iterable containing items of the same datatype. It could also be multiple iterables separated by commas.
  • key: The comparison of the iterable values is performed based on this function’s return value. 
  • functionName: Name of the key. In other words, the name of the function whose return value forms the basis of comparison.

Return Value: 

  • If a single iterable is passed, min() returns the iterable item, which gives the minimum value when evaluated for the function that’s the basis of comparison.
  • If multiple iterables are passed as arguments for min, min() compares the first items of all iterables and returns the iterable whose first value is the minimum. If all first values are equal, it compares the second values of all, and so on.

The min() Function in Python 3: Example

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:

Code


# 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)


Output


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. 

FAQs on the min() Function in Python

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.

Ready to Nail Your Next Coding Interview?

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!

Sign up now!

Last updated on: 
April 1, 2024
Author

Vartika Rai

Product Manager at Interview Kickstart | Ex-Microsoft | IIIT Hyderabad | ML/Data Science Enthusiast. Working with industry experts to help working professionals successfully prepare and ace interviews at FAANG+ and top tech companies

Attend our Free Webinar on How to Nail Your Next Technical Interview

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

The min() Function in Python

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image