Python List max() Method - GeeksforGeeks (2024)

Last Updated : 21 Aug, 2024

Summarize

Comments

Improve

Python list max() function returns the maximum value present in the list.

Example:

Python
#creating a listrand = [2,3,6,1,8,4,9,0]#printing max elementprint(max(rand))

Output

9

Definition of List max() Function

max() function in Python finds and returns the largest element in that list. Lets understand it better with an Example:

Python List max() Method Syntax:

max(listname)

Parameter

  • listname : Name of list in which we have to find the maximum value.

Returns : It return the maximum value present in the list.

How to Find Maximum Element in List in Python?

To find the maximum element in Python, you can use list max() function that returns max element from the list. Let’s understand it better with an example:

Python
#creating a listnumber = [54,67,12,33,69,32]#printing max elementprint(max(number))

Output

69

Python List max() Method Example

Lets look at some examples to find max element in Python list:

Example 1:

In this example, we are going to find the maximum integer value in the list. For that, we have to make a list of some random integers and then use the Python list max() function to find the maximum value in the list. Let’s implement this using code.

Code:

Python
# Declaring a list with random integers.list1 = [4, -4, 8, -9, 1]# Store maximum value in a variable# using Python list max() function.maxValue = max(list1)# Printing value stored in maxValue.print(maxValue)

Output

8

Example 2:

In this example, we are going to find the maximum value from the list of characters. This is done by following same procedure as in example 1.

Python
# Declaring a list with random integers.list1 = ['a', '$', 'e', 'E']# Store maximum value in a variable# using Python list max() function.maxValue = max(list1)# Printing value stored in maxValue.print(maxValue)

Output

e

In this case of character values the max value is determined on the basis of their ASCII values. For example ASCII value of ‘e’ is 101 and ‘E’ is 69 therefore ‘e’ is larger.

Example 3:

In this example, we are going to try to find the maximum value from the list of mixed values such as integers and strings.

Code:

Python
# Declaring a list with mixed values.list1 = ['a', '$', 'e', 3, 16]# Store maximum value in a variable# using Python list max() function.maxValue = max(list1)# Printing value stored in maxValue.print(maxValue)

As we can see in the output this code gives the error because finding the maximum value between integers and string is not supported by Python list max() function.

Output:

Traceback (most recent call last):
File "/home/b98ffbce53474d196bfb2d69e59d0873.py", line 6, in <module>
maxValue = max(lis)
TypeError: '>' not supported between instances of 'int' and 'str'

Below is the solution of above example no 3-

Solution :

We can find the maximum value between integers and string to convert integer into char but according to ASCII table only.

Python
# Declaring a list with mixed values.list1 = ['!', '$', '/', '3', '61']# Store maximum value in a variable# using Python list max() function.maxValue = max(list1)# Printing value stored in maxValue.print(maxValue)

Output

61

Note- integers lie between 48 to 57 from 1 to 9 in ascii table and it will consider only first char
of a string. Let if in above example there will be 16 instead of 61 then output will be 3 because
it will consider first char which is 1 in 16.

Complexity-

The time complexity of max() function in python is O(n) .

We have covered the definition, syntax and examples of max() function in Python. List max() method is very important for arithmetic projects and can save you a lot of time.

Hope you understood the use and functioning of max() function!

Python List max() Method – FAQs

What Does max() Do in Python?

The max() function in Python returns the largest item in an iterable (like a list or tuple) or the largest of two or more arguments. If the provided iterable is empty, it raises a ValueError. You can also pass a key function to max() to specify a method of determining the order.

Example:

numbers = [10, 20, 30, 40, 50]
result = max(numbers)
print(result) # Output: 50

How to Get Max Value from List in Python Without max Function?

To find the maximum value in a list without using the max() function, you can use a simple loop to iterate through the list and keep track of the largest number.

Example:

numbers = [10, 20, 30, 40, 50]
largest = numbers[0] # Start by assuming the first number is the largest

for number in numbers:
if number > largest:
largest = number

print(largest) # Output: 50

How to Get the Max of a List of Lists in Python?

To find the maximum value in a list of lists, you can use a nested approach with the max() function. You first find the maximum of each sublist and then find the maximum of those maxima.

Example:

lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
max_value = max(max(sublist) for sublist in lists)
print(max_value) # Output: 9

What is Max in NumPy?

In NumPy, max() is a function used to compute the maximum value of an array or along a specified axis. The function is highly optimized for performance and can handle very large arrays efficiently compared to native Python loops.

Example:

import numpy as np

array = np.array([1, 2, 3, 4, 5])
max_value = np.max(array)
print(max_value) # Output: 5

What are the MIN() and MAX() Functions in Python? What Do They Do?

In Python:

  • MIN(): The min() function returns the smallest item in an iterable or the smallest of two or more arguments. Like max(), it can also take a key function to determine how to compare the elements.
  • MAX(): The max() function, as discussed, returns the largest item in an iterable or the largest of two or more arguments.

Both functions are useful for finding the extremities in sequences of values and can be customized with a key parameter to specify how elements are compared.

Example using both min() and max():

numbers = [10, 20, 30, 40, 50]
print(min(numbers)) # Output: 10
print(max(numbers)) # Output: 50

These functions are essential tools in data analysis, optimization problems, and any scenario where you need to quickly assess the range or bounds of a set of values in Python



K

kamalsagar

Python List max() Method - GeeksforGeeks (1)

Improve

Previous Article

Python List index()

Next Article

Python min() Function

Please Login to comment...

Python List max() Method - GeeksforGeeks (2024)

References

Top Articles
The Best Crew Colors in GTA 5 Online
Demetrius Flournoy Big Meech
Garrison Blacksmith Bench
Lifewitceee
News - Rachel Stevens at RachelStevens.com
COLA Takes Effect With Sept. 30 Benefit Payment
Blackstone Launchpad Ucf
America Cuevas Desnuda
Naturalization Ceremonies Can I Pick Up Citizenship Certificate Before Ceremony
30% OFF Jellycat Promo Code - September 2024 (*NEW*)
Lenscrafters Westchester Mall
Www Thechristhospital Billpay
Ou Class Nav
83600 Block Of 11Th Street East Palmdale Ca
A.e.a.o.n.m.s
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
2021 Lexus IS for sale - Richardson, TX - craigslist
Dallas’ 10 Best Dressed Women Turn Out for Crystal Charity Ball Event at Neiman Marcus
Dexter Gomovies
Quest Beyondtrustcloud.com
Minecraft Jar Google Drive
Nissan Rogue Tire Size
Golden Abyss - Chapter 5 - Lunar_Angel
Missouri Highway Patrol Crash
Vandymania Com Forums
Lawson Uhs
Wnem Tv5 Obituaries
Jeff Nippard Push Pull Program Pdf
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Bra Size Calculator & Conversion Chart: Measure Bust & Convert Sizes
manhattan cars & trucks - by owner - craigslist
The Posturepedic Difference | Sealy New Zealand
Sam's Club Gas Price Hilliard
Chicago Pd Rotten Tomatoes
Hotel Denizen Mckinney
15 Downer Way, Crosswicks, NJ 08515 - MLS NJBL2072416 - Coldwell Banker
Whas Golf Card
Royals op zondag - "Een advertentie voor Center Parcs" of wat moeten we denken van de laatste video van prinses Kate?
Devotion Showtimes Near Mjr Universal Grand Cinema 16
Oreillys Federal And Evans
Vanessa West Tripod Jeffrey Dahmer
Flashscore.com Live Football Scores Livescore
Edict Of Force Poe
Joey Gentile Lpsg
Gary Lezak Annual Salary
Academic Notice and Subject to Dismissal
Walmart 24 Hrs Pharmacy
Go Nutrients Intestinal Edge Reviews
St Anthony Hospital Crown Point Visiting Hours
40X100 Barndominium Floor Plans With Shop
Clock Batteries Perhaps Crossword Clue
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 6036

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.