Last modified: Mar 25, 2026 By Alexander Williams

Find Max of 3 Numbers in Python Array

Finding the largest value is a common task. You often need to find the maximum of three specific numbers. This guide shows you how to do it with Python arrays.

We will cover several methods. Each method is useful in different situations. You will learn the best approach for your code.

Understanding the Python Array Module

First, know that Python has a built-in array module. It is different from a list. An array stores items of the same type. This makes it efficient for numbers.

You must import the module to use it. For a deeper dive, see our guide on Python Array vs List: Key Differences Explained.

Here is how to create a simple array of integers.


# Import the array module
import array as arr

# Create an array of integers ('i' is the type code)
my_array = arr.array('i', [15, 42, 7, 23, 56])
print(my_array)

array('i', [15, 42, 7, 23, 56])

Method 1: Using the max() Function

The simplest way is Python's built-in max() function. It returns the largest item from an iterable. You can use it directly on an array.

To find the max of the first three elements, you need to slice the array. Learn more about this in our Python Array Slice Guide.


import array as arr

numbers = arr.array('i', [89, 24, 51])
# Use max() on the array
maximum_value = max(numbers)
print(f"The maximum of the three numbers is: {maximum_value}")

The maximum of the three numbers is: 89

This method is clean and efficient. It is perfect when you need the max of all elements. For just three specific elements, use slicing first.

Method 2: Using a Loop for Manual Comparison

Sometimes you need more control. You can write a loop to compare values manually. This teaches you the logic behind finding a maximum.

You start by assuming the first element is the largest. Then you compare it with the next two.


import array as arr

data = arr.array('i', [33, 71, 19])
# Assume first element is max
max_num = data[0]

# Loop through the next two elements
for i in range(1, 3):  # Check indices 1 and 2
    if data[i] > max_num:
        max_num = data[i]

print(f"The largest number found by loop is: {max_num}")

The largest number found by loop is: 71

This method is great for learning. It also works if you need to track the index of the max value. It is a fundamental algorithm.

Method 3: Using Conditional Statements (if/elif)

For exactly three numbers, simple if and elif statements are very clear. You compare each number directly.

This approach is very readable. It is perfect for beginners who are learning conditional logic.


import array as arr

triplet = arr.array('i', [5, 99, 27])
a, b, c = triplet[0], triplet[1], triplet[2]  # Unpack values

if a >= b and a >= c:
    largest = a
elif b >= a and b >= c:
    largest = b
else:
    largest = c

print(f"The largest number from if/elif is: {largest}")

The largest number from if/elif is: 99

This is a straightforward and explicit method. It leaves no doubt about the comparison process. It works well for a fixed, small set of numbers.

Handling Edge Cases and Errors

Your code must be robust. What if the array has fewer than three elements? You should handle this case to avoid an IndexError.

Always check the Python Array Length before accessing elements. Here is a safe function.


import array as arr

def max_of_first_three(arr_instance):
    """Safely returns the max of the first three elements."""
    if len(arr_instance) == 0:
        return None  # Array is empty
    # Use slicing to get up to the first three elements
    elements_to_check = arr_instance[:3]
    return max(elements_to_check)

# Test with a small array
short_array = arr.array('i', [10, 20])
result = max_of_first_three(short_array)
print(f"Max of available elements: {result}")

Max of available elements: 20

This function is safe. It uses slicing to avoid index errors. It works for arrays of any size.

Conclusion

Finding the maximum of three numbers in a Python array is simple. You can use the built-in max() function for speed and simplicity.

For learning, use a manual loop or conditional statements. Always remember to handle arrays with fewer than three elements.

Choose the method that fits your needs. For more on working with arrays, explore our guide on Python Array Append: How to Add Elements.

Now you can find the maximum value in your data with confidence.