I have constantly found myself needing to extract the extremes from datasets.
Whether I was analyzing stock prices on the NYSE or processing census data from California, identifying the maximum and minimum values was always the first step.
In this tutorial, I will show you exactly how to find the largest and smallest numbers in Python using several efficient methods.
1. Use Python Built-in Functions
When you need a quick result, Python provides built-in functions that are incredibly optimized and easy to use.
I almost always reach for the max() and min() functions first because they handle iterables like lists and tuples natively.
Let’s look at a practical example involving the heights of some of the tallest buildings in the United States.
# Heights of famous US buildings in feet
building_heights = [1776, 1451, 1396, 1200, 1100, 1070]
# Finding the largest number in Python list
tallest_building = max(building_heights)
# Finding the smallest number in Python list
shortest_building = min(building_heights)
print(f"The tallest building height is: {tallest_building} feet")
print(f"The shortest building height is: {shortest_building} feet")I executed the above example code and added the screenshot below.

I prefer this method for most daily tasks because the code remains readable and Python handles the heavy lifting under the hood.
2. Use Python For Loop
Sometimes, you might be in a technical interview where the interviewer asks you to find these values without using built-in functions.
I remember early in my career, I had to demonstrate that I understood the logic of comparison by writing a manual loop.
This approach is great for understanding how Python compares values sequentially.
# Annual snowfall in inches for various US cities
snowfall_data = [45.2, 12.5, 88.1, 3.2, 55.4, 21.0]
# Initialize variables with the first element
largest_snowfall = snowfall_data[0]
smallest_snowfall = snowfall_data[0]
# Iterating through the Python list to compare values
for amount in snowfall_data:
if amount > largest_snowfall:
largest_snowfall = amount
if amount < smallest_snowfall:
smallest_snowfall = amount
print(f"Maximum annual snowfall recorded: {largest_snowfall} inches")
print(f"Minimum annual snowfall recorded: {smallest_snowfall} inches")I executed the above example code and added the screenshot below.

While this method is more “wordy” than using max(), it gives you total control over the comparison logic if you have complex data structures.
3. Use the Sort Method to Identify Python Extremes
Another trick I have used when I also need to organize the data is the sort() method.
By sorting a list in ascending order, the smallest number automatically moves to the first index, and the largest moves to the last.
This is particularly useful if you are working with financial data, like gas prices across different US states.
# Average gas prices in various US states (USD)
gas_prices = [4.55, 3.20, 3.89, 5.10, 3.45, 4.15]
# Sorting the Python list in place
gas_prices.sort()
# The smallest value is at the start
cheapest_gas = gas_prices[0]
# The largest value is at the end
expensive_gas = gas_prices[-1]
print(f"The lowest gas price found: ${cheapest_gas}")
print(f"The highest gas price found: ${expensive_gas}")I executed the above example code and added the screenshot below.

I should note that sorting changes the original list, so I only use this when the order of the data doesn’t need to be preserved.
4. Use NumPy for Large Datasets in Python
When I deal with massive datasets, like millions of rows of social security records or flight traffic data, standard Python lists can be slow.
In these cases, I switch to the NumPy library because it is significantly faster for numerical operations.
NumPy’s np.max() and np.min() are built for performance on large-scale arrays.
import numpy as np
# Simulating a large dataset of distances between US airports in miles
flight_distances = np.array([2475, 733, 1744, 597, 1240, 2588, 302])
# Finding extremes using NumPy functions
max_dist = np.max(flight_distances)
min_dist = np.min(flight_distances)
print(f"Longest flight distance: {max_dist} miles")
print(f"Shortest flight distance: {min_dist} miles")I executed the above example code and added the screenshot below.

If you are moving into data science or machine learning with Python, mastering NumPy for these operations is essential.
Find the Largest and Smallest Numbers in Python Dictionaries
In real-world Python development, data often comes in pairs, like a city name and its population.
I frequently use Python dictionaries to store this type of information, and finding the extreme values requires a slightly different approach.
We can use the key parameter in the max() and min() functions to tell Python exactly what to compare.
# Population of major US cities (approximate)
city_populations = {
"New York City": 8336000,
"Los Angeles": 3822000,
"Chicago": 2665000,
"Houston": 2302000,
"Phoenix": 1644000
}
# Finding the city with the largest population
largest_city = max(city_populations, key=city_populations.get)
# Finding the city with the smallest population in this set
smallest_city = min(city_populations, key=city_populations.get)
print(f"The largest city in this list is {largest_city} with {city_populations[largest_city]} people.")
print(f"The smallest city in this list is {smallest_city} with {city_populations[smallest_city]} people.")Using the .get method as a key is a very “Pythonic” way to extract the dictionary key associated with the maximum or minimum value.
Handle Empty Lists and Avoid Python Errors
I have seen many beginner Python scripts crash because they tried to find the maximum value of an empty list.
If you are pulling data from a live API, like a US weather service, the list might be empty if the service is down.
To prevent your Python program from crashing, I always recommend providing a “default” value.
# Scenario where no data is returned from an API
live_sensor_data = []
# Using the 'default' parameter to prevent a ValueError
max_val = max(live_sensor_data, default="No data available")
min_val = min(live_sensor_data, default=0)
print(f"Status: {max_val}")This simple addition has saved me from countless late-night debugging sessions when data streams go silent.
Compare Multiple Arguments Directly in Python
Sometimes you aren’t dealing with a list at all; you might just have three or four separate variables you want to compare.
I love that the Python max() function allows you to pass variables directly as arguments without wrapping them in a list.
# Comparing Q1, Q2, and Q3 profits for a US small business
q1_profit = 45000
q2_profit = 52000
q3_profit = 48000
# Direct comparison in Python
best_quarter = max(q1_profit, q2_profit, q3_profit)
print(f"The highest profit this year was: ${best_quarter}")This makes the code much cleaner when you have a fixed number of items to compare.
In this tutorial, I showed you several different ways to find the largest and smallest numbers in Python.
Whether you use the built-in functions for speed, a loop for custom logic, or NumPy for big data, you now have the tools to handle any numerical range.
I personally find that using max() and min() with a default value is the safest and most efficient path for most Python projects.
I hope you found this tutorial helpful! If you have any questions or want to see more Python tips, feel free to explore my other guides.
- Get Keys of a Dictionary in Python
- Print a Dictionary in Python
- Sort a Python Dictionary by Key Alphabetically
- Reverse a Dictionary 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.