In this tutorial, we will look at how to get the max value and its corresponding index in a tuple in Python with the help of some examples.
How to get the max value in a tuple?
You can use the Python built-in max()
function to get the max value in a tuple. Alternatively, you can also iterate through the tuple to find the maximum value.
Let’s look at the two methods with the help of examples.
Using the max()
function
The Python built-in max()
function returns the maximum value in an iterable. Pass the tuple as an argument to get its maximum value.
# create a tuple t = (1, 7, 5, 2, 4) # get max value in tuple print(max(t))
Output:
7
We get the maximum value of the tuple t
as 7
.
To get the index corresponding to the max value, you can use the tuple’s index()
function. This function returns the index of the first occurrence of the element inside the tuple. If the element is not present, it raises an error.
# create a tuple t = (1, 7, 5, 2, 4) # find max value max_val = max(t) # display its index print(t.index(max_val))
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
1
We get the index of the max value as 1
.
Get max value using iteration
Alternatively, you can iterate through each value in the tuple and keep track of the maximum value to find the max value in the tuple. You can also keep track of the index of the maximum value.
Let’s look at an example.
# create a tuple t = (1, 7, 5, 2, 4) # find max value using loop max_val = t[0] max_val_idx = 0 for i in range(len(t)): if t[i] > max_val: max_val = t[i] max_val_idx = i # display the max value print(max_val) # display its index print(max_val_idx)
Output:
7 1
We get the maximum value and its index in the tuple.
Note that if your tuple contains more than one occurrence of a value, the index()
function will only return its first occurrence. To get all the indexes of occurrence, you iterate through the elements in a list comprehension.
# create a tuple t = (1, 7, 5, 2, 7) # get all indexes of 7 idx = [i for i in range(len(t)) if t[i] == 7] print(idx)
Output:
[1, 4]
We get all the indexes of occurrence for the value 7
in the tuple t
.
You might also be interested in –
- Python – Find Max Value and its Index in List
- Get Average of Elements in a Python Tuple
- Get Median of a Tuple in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.