How to find the largest element in an array in Python [3 Methods]

Do you want to find the largest element in an array? In this NumPy tutorial, I will explain how to find the largest element in an array in Python using different methods with some illustrative examples.

To find the largest element in an array in Python, we can utilize various methods like the np.max() function from NumPy for efficient computation, sorting the array and selecting the last element, or employing a for loop to iterate through the elements and find the maximum.

Find the largest element in an array in Python

There are three different methods to find the largest element in an array in Python:

  1. np.max() function
  2. sort() function
  3. Using for loop

Let’s see them one by one using some illustrative examples:

1. Python program to find largest element in an array using np.max() function

The np.max() function is used to find the maximum value in an entire array or along a specified axis of an array. It’s used when we need the single largest value from the array.

Here is the code to find the largest element in an array using the np.max() function from the NumPy library

import numpy as np

temperatures = np.array([88, 90, 87, 92, 93, 91, 89])
highest_temperature = np.max(temperatures)
print(f"Highest temperature: {highest_temperature}°F")

Output:

Highest temperature: 93°F

The screenshot provided below showcases the output generated after running the code in the PyCharm editor.

How to find the largest element in an array in Python

2. Python program to print the largest element in an array using the sort() function

By sorting the array using the sort() function, the largest element will be positioned at the last index. After sorting, we can simply pick the last element. This method is straightforward but can be less efficient for large arrays, as sorting the entire array can be time-consuming.

import numpy as np

river_lengths = np.array([2340, 1450, 2525, 3710, 3100])
river_lengths.sort()
longest_river = river_lengths[-1]
print(f"Longest river: {longest_river} miles")

Output:

Longest river: 3710 miles

Below is a screenshot depicting the output, captured after the code was run in the PyCharm editor.

How do you find the largest element in an array in Python

3. Find Largest element in an array using Python using a for loop

We can iterate through each element of the array using the for loop in Python, comparing each one with a variable that keeps track of the largest element found so far. It’s a manual approach that doesn’t rely on Python’s built-in functions.

Here is the code to find the largest element in an array in Python using the for loop:

import numpy as np

building_heights = np.array([1050, 1400, 1250, 1550, 2000, 1450])
tallest_building = building_heights[0]

for height in building_heights:
    if height > tallest_building:
        tallest_building = height

print(f"Tallest building: {tallest_building} feet")

Output:

Tallest building: 2000 feet

Following the execution of the code in PyCharm, the resulting output is captured in the screenshot displayed below.

How do you print the highest value in an array in Python

4. Python program for largest element in a 2D Array

Finding the largest element in a 2D array (or matrix) in Python can be efficiently done using NumPy:

import numpy as np

# 2D array representing rainfall data for New York, Los Angeles, and Chicago
rainfall_data = np.array([[40, 42, 38], [15, 12, 17], [35, 36, 39]])
highest_rainfall = np.max(rainfall_data)
print(f"The highest average annual rainfall recorded was: {highest_rainfall} inches")

Output:

The highest average annual rainfall recorded was: 42 inches

Following the execution of the code within the Pycharm editor, a screenshot of the outcome is displayed below.

Python program to find the largest element in an array

Conclusion

Here, I have explained three distinct approaches to find the largest element in an array in Python: using the np.max() function for quick and efficient computation, sorting the array and selecting the last element, and iterating through the array with a for loop for a more manual method, also in a 2D array.

Each method caters to different scenarios and efficiency needs, providing a comprehensive toolkit for handling this common task in Python programming.

You may also like to read: