• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python max() function – Get Maximum of List or Dictionary with max() Function

Python max() function – Get Maximum of List or Dictionary with max() Function

February 7, 2022 Leave a Comment

To get the maximum of a list of numbers or strings in Python, we can use the Python max() function. You can use the max() function in Python to get the maximum value of numbers in a list, strings in a list, and values in a dictionary.

list_of_numbers = [10,32,98,38,47,34]

print(max(list_of_numbers))

#Output
98

There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.

One such task is to find the maximum value of a collection of numbers or strings.

We can use the Python max() function to find the maximum, or biggest, value in an iterable (list, dictionary, set, etc.) made up of numbers or strings.

Let’s say we have the following list of numbers in Python. We can use the Python built-in max() function to get the maximum value of the list in the following way:

list_of_numbers = [10,32,98,38,47,34]

print(max(list_of_numbers))

#Output
98

We can also call the max() function with multiple arguments. In this case, max() returns the biggest value of the arguments.

print(max(10,32,98,38,47,34))

#Output
98

For a collection of strings, the biggest value is determined after sorting alphabetically.

print(max("Mike","Alex","Patricia"))

#Output
Patricia

If you instead want to find the minimum value of an iterable, you can use the Python min() function.

If you are using pandas in Python and want to get the maximum value of a column or DataFrame, you can use the pandas max() function.

How to Find Maximum of Two Numbers Using Python max() Function

In Python, we can easily find the maximum of two numbers using the max() function.

Below are a few examples of getting the max value between two numbers using max().

print(max(10,32))
print(max(-30,4))
print(max(40,-21))
print(max(8,-22))
print(max(98,99))

#Output
32
4
40
8
99

How to Find Biggest Value in List of Numbers Using Python max() Function

We can use the Python max() function to get the biggest value in a list of numbers easily. To find the max value of a list, we just pass the list to max().

Let’s say we have the following list of numbers.

list_of_numbers = [10,32,98,38,47,34]

To get the maximum value of a list of numbers in Python, we use the max() function.

list_of_numbers = [10,32,98,38,47,34]

print(max(list_of_numbers))

#Output
98

How to Get Maximum Value in List of Strings Using Python max() Function

We can use the Python max() function to get the biggest value in a list of strings easily. To find the max value of a list, we just pass the list to max().

The biggest value from a list of strings is the first string in alphabetical order from Z to A, meaning the string that starts with a letter closest to Z will be the biggest. Also, note that uppercase letters come before lowercase letters.

Let’s say we have the following list of strings.

list_of_strings = ["Low","High","Better","Worse"]

To get the maximum value of a list of strings in Python, we use the max() function.

list_of_strings = ["Low","High","Better","Worse"]

print(max(list_of_strings))

#Output
Worse

How to Find Maximum Value in Dictionary Using Python max() Function

With the Python max() function, we can find the maximum value in a dictionary.

Let’s say we have the following dictionary.

dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }

To get the max value in a dictionary, we pass the dictionary to max() as shown in the following Python code.

dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }

print(max(dict))

#Output:
4

We can also get the maximum value of the keys in a dictionary. To do so, we need to pass a lambda expression for max() as the “key”.

Below is how to find the max value of the keys in a dictionary in Python.

dict = { "Alex": -3, "John":5, "Tammy":-10, "Jim":4 }

print(max(dict, key = lambda k: dict[k]))

#Output:
Tammy

Hopefully this article has been useful for you to learn how to use the Python max() function to get the maximum value of a list of numbers or strings.

Other Articles You'll Also Like:

  • 1.  Sort Series in pandas with sort_values() Function
  • 2.  Python Negative Infinity – How to Use Negative Infinity in Python
  • 3.  Using Python to Check if Queue is Empty
  • 4.  Convert pandas Series to Dictionary in Python
  • 5.  Adjusting Python Turtle Screen Size with screensize() Function
  • 6.  Using Python to Count Number of Lines in String
  • 7.  Draw Rectangle in Python Using turtle Module
  • 8.  Check if String Contains Numbers in Python
  • 9.  Get Day Name from Datetime in pandas DataFrame
  • 10.  How to Check if a String Contains Vowels in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy