• 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 / Using Python to Find Maximum Value in List

Using Python to Find Maximum Value in List

October 11, 2022 Leave a Comment

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

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

print(max(list_of_numbers))

#Output
98

When working with different collections of data in Python, the ability to be able to summarize and find properties of these pieces of data are valuable.

One such case is if you want to find the maximum value of a list in Python.

You can use the Python max() function to find the maximum, or biggest, value in a list made up of numbers or strings.

Let’s say we have the following list of numbers in Python. You 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

For a collection of strings, the biggestvalue is determaxed after sorting alphabetically.

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

#Output
Patricia

Using Lambda Expression with max() Function on List of Objects in Python

Depending on the objects in your list, it might make sense that you need to apply a function or want to find the maximum value based on a certain attribute of the objects in a list.

One cool thing is that you can pass a lambda expression to the “key” parameter to instruct max() what to use to find the maximum value.

One use of a lambda expression with max() is if you want to find the maximum value in list of objects.

For example, if we have a list of numbers and want to take the maximum after applying the absolute value to each element, we can use a lambda.

Below shows a simple example of using a lambda expression with max() in Python.

example_list = [0, 3, 1, -3, -5, 4]

print(max(example_list, key=lambda x:abs(x)))

#Output:
5

Another example is let’s say we have a list of dictionaries and want to find the maximum value over our list for the key “height”.

You can easily do this with “key” and a lambda expression.

Below is an example of how to use a lambda expression with max() to get the maximum value of a list of dictionaries for a given key.

data = [{"name":"Andrew", "age":10, "height":50}, {"name":"Sally", "age":9, "height":45}, {"name":"Jose", "age":11, "height":52}]

print(max(data,key=lambda x: x["height"]))

#Output:
{"name":"Jose", "age":11, "height":52}

If you have a list of tuples, you can use “key” and a lambda as well to get the maximum value of a given position in the tuples.

Below is an example of how to use a lambda expression with max() to get the maximum value of a list of tuples for the first element.

data = [(0,'a'),(3,'d'),(2,'r'),(1,'m')]

print(max(data, key=lambda x: x[0]))

#Output:
(3,'d')

Finding Minimum Value of List with min() in Python

If you want to go the other way and find the min value of a list, you can use the Python min() function.

min() works the same as max and returns the smallest value of the list.

Below is a simple example showing you how to use min() in Python.

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

print(max(list_of_numbers))

#Output
10

Hopefully this article has been useful for you to learn how to get the maximum value of a list in Python with max().

Other Articles You'll Also Like:

  • 1.  Ceiling Division in Python
  • 2.  Using pandas to_csv() Function to Append to Existing CSV File
  • 3.  How to Read XLSX File from Remote Server Using Paramiko FTP and Pandas
  • 4.  Convert String into Tuple in Python
  • 5.  Truncate String in Python with String Slicing
  • 6.  Check if Variable is Tuple in Python
  • 7.  Remove Leading Zeros from String with lstrip() in Python
  • 8.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 9.  Write Variable to File Using Python
  • 10.  Combine Sets 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