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:

- np.max() function
- sort() function
- 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.

### 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.

### 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.

### 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.

## 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:

- Python program to print the smallest element in an array
- Python program to print the duplicate elements of an array
- NumPy array to a string in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.