How to find largest or max value in a list in python

This article will explain two different ways in which you can find max value in a list or array in python.

Method 1: Using max function
Python has a built-in function max which accepts an iterable as argument and returns the maximum value present in it.
Iterable means a data structure that can be iterated such as a list, array or dictionary. In case of a dictionary, max value will be from its keys or values as desired.
Examples
Below are the examples of max function.
Find largest number in list
Supply a list of numbers as an argument to max function. It will return the largest(or max) number in the list.

numbers = [10, 20, 35, 60, 33]
largest = max(numbers)
print("Largest number in list is:", largest)

Output of above code is

Largest number in list is: 60

max also accepts a list of decimal(or floating) numbers and will return the largest number from the list.

Find largest string
For a list of string values, max will return the largest string as per the alphabetical order of its characters in ASCII table.
Thus, it will compare the first letter of each item in list. Element whose first character lies last in alphabetical sequence will be treated as the largest element.
If the first letter of two words are same, then the second letter is given preference.
In case of lower and upper case words, lower case characters will be considered larger. That is, “english” will be larger as compared to “English”.

strings = ["Python","fun", "is", "learning"]
largest = max(strings)
print("Largest word is:", largest)

Largest word is: learning

Above example considers “learning” as larger than “Python” since lower case characters are placed after upper case letters in ASCII table.
Find largest key-value in dictionary
For finding a largest key or value in a dictionary, supply the list of keys or values to max function using keys() or values() function of a dictionary respectively. Example,

dict = {"A" : 0, "Z" : -1, "C" : 2, "X" : -3}
# find max key
max_key = max(dict.keys())

# find max value
max_value = max(dict.values())
print("Largest key:", max_key)
print("Largest value:", max_value)

Largest key: Z
Largest value: 2

If you simply supply a dictionary to max, then it will return the largest key since keys() is the default. Example,

dict = {"A" : 0, "Z" : -1, "C" : 2, "X" : -3}
# only supply dictionary object
max_value = max(dict)
print("Largest value:", max_value)

This example will output

Largest value: Z

Default max value
max function expects a non-empty iterable object as argument. If the object is empty, it throws an error.

ValueError: max() arg is an empty sequence

To avoid the error, you can provide a default value that max should return in case of empty object using default keyword followed by the value to return. Example,

# empty list
numbers = []
# max with default value
m = max(numbers, default=10)
print("Default value is:", m)

Output of above example is

Default value is: 10

Method 2: Using for loop
A loop can also be used to find the max element in a list or in dictionary keys/values. Initialize a variable with value 0.
Iterate over the list and in every iteration compare the current list value with the variable. If the list item is greater than the variable, update the variable with this list item.
At the end of all iterations, the variable shall have the largest list value. Example,

# list of numbers
numbers = [10, 20, 35, 60, 33]
# variable to hold max list value
max_value = 0
# iterate list
for item in numbers:
   if item > max_value:
      # update variable
      max_value = item
print("Largest list value:", max_value)

This example prints

Largest list value: 60

For finding the max value in dictionary keys or value, iterate over the dictionary using the list of its keys or values in a similar manner.
Hit the clap if this post served its purpose.

0

Leave a Reply

Mark Your Impression

  Subscribe  
Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.