Programming tips for everyone

# Python How to Find the Largest Number in a List To find the largest number in a list in Python:

1. Set the first element as the largest number candidate.
2. Loop through the list of numbers.
3. Update the largest number candidate if any number is greater than it.

Here is how you can do it with a for loop:

```heights = [100, 2, 300, 10, 11, 1000]

largest_number = heights

for number in heights:
if number > largest_number:
largest_number = number

print(largest_number)
```

Output:

`1000`

Now you know how to find the largest element of a list from scratch. But it is good to know there is a built-in function `max()` that also does the job for you.

## The max() Function — Find the Largest Element of a List

In Python, there is a built-in function `max()` you can use to find the largest number in a list.

To use it, all you need to do is call the `max()` on a list of numbers. This function then returns the greatest number in that list.

Here is an example of how:

```heights = [100, 2, 300, 10, 11, 1000]

max_height = max(heights)

print(max_height)```

Output:

`1000`

## Alternative Approaches

Now you know the two most basic ways to find the largest number in a list.

Let’s take a look at some uncommon approaches.

### Reduce() Function

You can also use the `functools` `reduce()` function to find the largest number in a list.

Before we do that, it is important to understand how the `reduce()` function works.

`reduce(function, iterable)`

The reduce function takes two parameters:

1. A function that is applied for each element of an iterable.
2. An iterable, such as a list.

It then:

• Takes the first two elements of a sequence and calls the function on them.
• Takes the previous result, and calls the function on the result and the next number in the list.
• This process continues until there are no elements left in the list.

#### Reduce() with the built-in max() Function

Here is an example of how you can use reduce to find the largest number in a list:

```from functools import reduce

heights = [100, 2, 300, 10, 11, 1000]

max_height = reduce(max, heights)

print(max_height)```

Output:

`1000`

This approach works such that it applies the `max()` function for each element as described above.

• It starts with the two first elements and finds the largest of the two
• Then takes the result and compares it with the third element.
• This process continues until there are no numbers left in the list.

#### Reduce() with a Custom Max Function

Another way you can use `reduce()` to find the largest number in a list is by defining the `max()` function yourself.

For example:

```from functools import reduce

heights = [100, 2, 300, 10, 11, 1000]

def my_max(x, y):
if x < y:
return y
else:
return x

max_height = reduce(my_max, heights)

print(max_height)```

Output

`1000`

#### Reduce() with a Lambda Function

And the third approach is to use `reduce()` with a lambda expression. This means you define the max function inline in the `reduce()` function call.

For example:

```from functools import reduce

heights = [100, 2, 300, 10, 11, 1000]

max_height = reduce(lambda x, y: y if x < y else x, heights)

print(max_height)```

Output:

`1000`

The `lambda x, y: y if x < y else x` part does the same as the `my_max` function in the previous example. Notice how the if-else statement is shortened to a one-liner expression.

### Find The Largest Number Using a Heap Queue

The built-in `heapq` module in Python comes in with an implementation of the priority queue algorithm.

In short, a heap is a binary tree where each parent node has a value less than or equal to the value of its children.

You can use the `heapq.nlargest()` function to figure out the largest number(s) in a list.

For example:

```import heapq

heights = [100, 2, 300, 10, 11, 1000]

max_height = heapq.nlargest(1, heights)

print(max_height)```

Output:

`1000`

## Conclusion

Today you learned how to find the largest number in a list.

First, you used the “brute-force” method to loop through the list while keeping track of the largest element.

Then you saw how the built-in `max()` function does the job for you.

Finally, you saw uncommon alternatives, such as using `reduce()` with lambdas or a heap queue.

Thanks for reading. I hope you find it useful.

Happy coding!