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 max() 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 max() function returns the maximum 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 max() in C/C++, in Python, max() can take in any type of objects and return the largest object.
The function that will form the basis for our maximum value calculation can also be specified. For example, we can evaluate the max value of strings based on lexicography, string length, etc.
For strings, it by default returns the lexicographically largest value. However, we can specify the basis of max calculation to be another function like “len” for string length. Finally, when we pass an iterable to max() in Python, it returns the largest item in the iterable.
Let us look at the syntax for the Python max() function when used with objects and when used with iterables:
max(item1, item2,...itemN, key = functionName)
Parameters :
Return Value:
max(iterable(s), key = functionName)
Parameters:
Return Value:
Here, we take a look at how you can use the Python function max() in the context of the data structure list, dictionary, string, or integer next time you need it:
# Usage of max() in Python
# Example of integers
intValue1 = 20
intValue2 = 60
intValue3 = 40
intValue4 = 100
# Getting the maximum value out of all four values
maxValue = max(intValue1, intValue2, intValue3, intValue4)
print("The integer of the maximum value: ")
print(maxValue)
# Example of strings
stringValue1 = "we"
stringValue2 = "love"
stringValue3 = "interview"
stringValue4 = "kickstart"
# Getting the lexicographically largest string
maxValue = max(stringValue1, stringValue2, stringValue3, stringValue4)
print("The lexicographically largest string: ")
print(maxValue)
# Getting the string with the maximum length (basis of comparison value comes from function len)
maxValue = max(stringValue1, stringValue2, stringValue3, stringValue4, key=len)
print("The string with the maximum length: ")
print(maxValue)
# Getting the lexicographically maximum character in a string
stringExample = "interviewkickstart"
print("The lexicographically maximum character in interviewkickstart: ")
maxValue = max(stringExample)
print(maxValue)
# Example of list
# String list
stringListExample = ["we", "love", "interview", "kickstart"]
maxValue = max(stringListExample)
print("The lexicographically largest string in the list: ")
print(maxValue)
maxValue = max(stringListExample, key=len)
print("The maximum length string in the list: ")
print(maxValue)
# Int List
intListExample = [20, 60, 40, 100]
maxValue = max(intListExample)
print("The maximum value of an integer in the list:")
print(maxValue)
# Finding the index of the maximum value in the int list
indexmaxValue = intListExample.index(max(intListExample))
print("The maximum value", maxValue, " is at position ", indexmaxValue + 1)
# Example of dictionary, also showing that if the container is empty, the max value will show default value
emptyDictExample = {}
maxValue = max(emptyDictExample,
default={1: "Yes!"})
print("Doing max() on an empty dictionary with a given default value gives default value as max value: ")
print(maxValue)
# Example of using multiple iterables of type int in max()
intListExample1 = [10, 100, 1000, 10000]
intListExample2 = [200, 600, 400, 800]
maxValue = max(intListExample1,intListExample2)
print("Passing multiple iterables to max() returns the iterable with the maximum first element: ")
print(maxValue)
# Example of using multiple iterables of type string in max(). Also using the default key and then len() as the key in the example
stringValueA = "we"
stringValueB = "love"
maxValue = max(stringValueA, stringValueB)
print("The max value when both strings are passed with a default key (the lexicographically largest first character): ")
print(maxValue)
maxValue = max(stringValueA, stringValueB, key=len)
print("The max value when both strings are passed with the length function as key: ")
print(maxValue)
The integer of the maximum value:
100
The string with lexicographically largest character:
we
The string with the maximum length:
interview
The lexicographically maximum character in interviewkickstart:
w
The lexicographically largest character of the string in the list:
we
The maximum length string in the list:
interview
The maximum value of an integer in the list:
100
The maximum value 100 is at position 4
Doing max() on an empty dictionary with a given default value gives default value as max value:
{1: 'Yes!'}
Passing multiple iterables to max() returns the iterable with the maximum first element:
[200, 600, 400, 800]
The max value when both strings are passed with a default key (the lexicographically largest first character):
we
The max value when both strings are passed with the length function as key:
love
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, max(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 maximum element in a container in Python?
You can find the largest element in a container in Python by using the max() 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 max() an in-built function in Python?
Yes, max() is an in-built function in Python.
Q5. What value does the Python min() function return?
The Python max() function returns the object with the maximum 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 the field of 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