Python – Find Maximum of Items in a Tuple
To find maximum of items in a Tuple in Python, use max() builtin function. We can also provide a key function for max() to transform the items and then find the maximum of those values.
Examples
1. Maximum of Tuple of Integers
In the following program, we take a tuple x
with integer values, and find the maximum item in this tuple.
Python Program
</>
Copy
aTuple = (2, 5, 8, 1, 4, 3)
result = max(aTuple)
print('Maximum :', result)
Output
Maximum : 8
2. Maximum of Tuple of Strings based on Length
Here key is length of the item. To find maximum of strings based on length, call max() function and send len() function as key parameter.
Python Program
</>
Copy
aTuple = ('Tea', 'Banana', 'Pomegranate')
result = max(aTuple, key=len)
print('Maximum :', result)
Output
Maximum : Pomegranate
Conclusion
In this Python Tutorial, we learned how to find the maximum of items in a Tuple in Python using max() function.