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 max() Function in Python

Last updated by Swaminathan Iyer on Sep 25, 2024 at 11:02 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 max() 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 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:

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

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

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.

The max() Function in Python: Syntax

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

The max() function with objects: Syntax


max(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 maximum value for the function that forms the basis of comparison is returned.
  • If a single string is passed as an object, the lexicographically largest character is returned.

The max() function with iterables: Syntax


max(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, max() returns the iterable item, which gives the maximum value when evaluated for the function that’s the basis of comparison.
  • If multiple iterables are passed as arguments for max, max() compares the first items of all iterables and returns the iterable whose first value is the maximum. If all first values are equal, it compares the second values of all, and so on.

The max() Function in Python: Example

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:

Code


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

Output


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. 

FAQs on the max() Function in Python

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.

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 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!

Sign up now!

Last updated on: 
October 10, 2024
Author

Swaminathan Iyer

Product @ Interview Kickstart | Ex Media.net | Business Management - XLRI Jamshedpur. Loves building things and burning pizzas!

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
First Name Required*
Last Name Required*
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 max() 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