Data Structures and Algorithms

Finding the Maximum Number in a List (

1 day, 20 hours ago ; F(visit_count) + Value(1) views
Share this

Finding the Maximum Number in a List (The Pythonic Way)


When solving problems using Python, understanding how to write optimal and simple code is key. 

At Python Haven, we specialize in breaking down complex programming problems into clear, easy-to-follow solutions.

In this article, we'll explore how to find the maximum number in a list most cleanly and efficiently.

We'll start with a simple approach and explain every single step like you're hearing about lists and numbers for the very first time. Whether new to Python or preparing for coding interviews, this walkthrough will help you understand the logic behind scanning lists for the highest number.

How to Think About the Problem

Imagine you have a row of toy blocks, each with a number on it. You want to find the biggest number out of all the blocks. That's all this code does — it walks through each block, looks at the number, and keeps track of the biggest one it has seen so far.

The Code: Explained Like You're 2 Years Old

from typing import List

def max_number(nums: List[int]) -> int:
    """
    Finds and returns the biggest number from a list.

    Args:
        nums (List[int]): A list of numbers (could be big or small).

    Returns:
        int: The biggest number found in the list.
    """
    max_val = -float("inf")  # Start by assuming the biggest number is super, super small

    for num in nums:  # Look at every number one by one
        if num > max_val:  # If this number is bigger than what we thought was biggest...
            max_val = num  # then this number is the new biggest!
    
    return max_val  # When done, tell us what the biggest number was


How This Works

We start by saying: "Let's pretend the biggest number is very small."

Then we look at each number one at a time.

If a number is bigger than the one we have, we replace our guess with the new number.

The biggest number will be saved and returned when we finish going through the list.

Unit Tests That Make Sure It Works

Here are some simple and large examples to test if our function always gives the correct answer.

import unittest

class TestMaxNumber(unittest.TestCase):
    
    def test_max_number1(self):
        self.assertEqual(7, max_number([3, 1, 7, 2, 5]))
    
    def test_max_number2(self):
        self.assertEqual(-2, max_number([-5, -2, -9]))

    def test_max_number3(self):
        self.assertEqual(9999, max_number([1]*10**5 + [9999]))

    def test_max_number4(self):
        self.assertEqual(1000000, max_number(list(range(1, 10**6 + 1))))

    def test_max_number5(self):
        self.assertEqual(1000000000, max_number([10**9]*10**6))

if __name__ == "__main__":
    unittest.main()


What's Happening Here?

  • We check simple lists.
  • We check lists with all negative numbers.
  • We check lists with thousands or millions of numbers to ensure the function is fast and works well.

Performance: Fast and Light

Time Complexity: O(n)

We only look at each number once. That means the time it takes grows linearly with the numbers in the list.

Space Complexity: O(1)

We don't create new lists or store lots of data. We only keep one number in memory at any time — the biggest one.

This is the best possible performance you can get without using Python’s built-in max() function

 Visual Breakdown

Here's a simple diagram of how the code scans the list:

List: [3, 1, 7, 2, 5]
Scan:  ↑   ↑   ↑   ↑   ↑
Max :  3   3   7   7   7


At every step, we update max_val only if the current number is bigger than before.

Want to Keep Getting Better at Python?

This technique shows how simple logic and careful thinking can lead to efficient Python solutions. Once you're confident with this, try creating your versions of min_number, average_number, or even a version that skips negative values!

If you enjoyed this, check out more algorithm deep-dives and coding patterns on PythonHaven.com.

Ready to Maximize Your Python Skills?

If you followed this guide, you now understand how to find the maximum number in a list efficiently and clearly. Try this with different lists, test your skills, and move on to more advanced problems.

Want more hands-on articles like this one? 

Subscribe to our newsletter or follow us on social platforms for weekly updates on coding best practices!

Become a member
Get the latest news right in your inbox. We never spam!

Read next

Moving Zeros to the End in Python

Moving Zeros to the End in Python: From Slow to Super Fast As a software engineer and algor… Read More

1 day, 19 hours ago . 182 views