Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How to find the highest value of an iterable

Maria Elijah

Overview

In Python, the max() is used to find the highest item in an iterable. An iterable is an object of any type that can be iterated using a loop. For example, list or tuple. Iterables can have any type of value such as integers, strings, and so on. We pass our iterable to max() method in Python and it returns the highest value. It can also be used to return the highest value between two values.

Syntax

max(n1, n2, n3, ...)
Or
max(iterable) 

Parameter

The max() function accepts an iterable as a parameter to find its maximum value.

Return value

The max() function in python returns the highest value of the provided iterable.

Example 1

These examples show the use of max() function in Python.

# create a list
scores = [50, 20, 9, 70, 45, 90, 5]
# call max() function and store result
result = max(scores)
# display result
print(f"The highest score : {result}")
Example to use max() function

Explanation

  • Line 2: We create a list named scores.
  • Line 4: We use the max() function to find the highest value from the scores and store the highest value in result variable.
  • Line 6: We print the highest value.

Example 2

This example shows the use of the max() function to find the highest string in a list.

Note: The function checks the list alphabetically

# create a list
colors = ['Red','Orange', 'Blue', 'Pink']
# store result
result = max(colors)
# display result
print(f"The highest color: {result}")

Explanation

  • Line 2: We create a list of strings named colors.
  • Line 4: We use the max() function to find the highest value from the colors and store the highest value in result variable.
  • Line 6: We display the result.

RELATED TAGS

python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring