How to Find the Maximum Value in Python Using max()

I still remember the week I stopped tracking leave in a shared Excel file. I once helped a 10-person U.S. team whose HR tracker kept breaking, since someone overwrote someone else’s row or forgot to update a formula every month. So I built a tiny Python command-line app to log leave requests, backed by a local SQLite database on my own machine.

While building it, I kept running into one question: which leave request is the longest, or whose balance is highest? That question led me back to one function. This guide walks through how to find the maximum value in Python using max(), with the same leave-request app as our running example.

By the end, you will know how to use max() with numbers, strings, lists, dictionaries, and full database records. Let’s start with the basics.

What Is the max() Function in Python?

max() is a built-in function in Python, meaning you don’t need to import anything to use it. It looks at a group of values and returns the single largest one. No loops, no manual comparisons.

Here’s the simplest form:

print(max(12, 45, 7))

Output:

45

Python compares each number and hands back the biggest one, whether you’re comparing exam scores, prices, or days someone requested off.

Basic Syntax of max()

The max() function accepts values in two main ways:

max(iterable, *, key=None, default=None)
max(arg1, arg2, *args, key=None)

An iterable is any object you can loop over, like a list, tuple, set, or dictionary. You can pass one iterable, or pass multiple separate values (positional arguments) directly. Note that default only works with the single-iterable form; it has no effect with separate positional arguments.

Both of these work:

scores = [88, 92, 79, 95, 67]
print(max(scores))

print(max(88, 92, 79, 95, 67))

Output:

95
95

I executed the above example code and added the screenshot below.

Find the Maximum Value in Python Using max()

The first line passes a list as a single iterable. The second passes the same numbers as five separate arguments, and Python treats them identically.

Finding the Maximum Value in Python Using max() With Different Data Types

Let’s go through the common data types you’ll work with: numbers (days requested), strings (employee names), and full records pulled from a database.

Numbers

This is the most common use case: days off, ages, hours worked, prices, anything numeric.

days_requested = [4, 9, 1, 6, 3]
print(max(days_requested))

Output:

9

That line tells me someone requested 9 days off, the longest of the batch.

Strings

Python can also compare strings alphabetically, based on character codes, walking letter by letter to find the one that comes last.

employees = ["Emma", "Liam", "Noah", "Austin"]
print(max(employees))

Output:

Noah

I executed the above example code and added the screenshot below.

How to Find the Maximum Value in Python Using max()

“Noah” comes after the other names alphabetically, so it wins. This is handy for finding the last name alphabetically, though it’s rarely why you’d reach for max() in a real app.

Lists, Tuples, and Sets

max() works the same way across a list (an ordered, changeable collection), a tuple (an ordered, unchangeable collection), and a set (an unordered collection with no duplicates). If you’re unsure how these three differ, it’s worth reading about how to compare lists, tuples, sets, and dictionaries in Python before going further.

leave_days_list = [4, 9, 1, 6, 3]
leave_days_tuple = (4, 9, 1, 6, 3)
leave_days_set = {4, 9, 1, 6, 3}

print(max(leave_days_list))
print(max(leave_days_tuple))
print(max(leave_days_set))

Output:

9
9
9

All three return the same answer, since max() doesn’t care about container type.

Using max() With Dictionaries in Python

Dictionaries trip up beginners because a dictionary stores key-value pairs, and max() needs to know which part you want.

Say I track total leave days per employee this year:

leave_balance = {"Emma": 5, "Liam": 12, "Austin": 3, "Denver": 8}

print(max(leave_balance))
print(max(leave_balance.values()))
print(max(leave_balance, key=leave_balance.get))

Output:

Liam
12
Liam

The first call, max(leave_balance), loops over the dictionary’s keys by default, returning “Liam” simply because that name is alphabetically last — a coincidence, not the answer we wanted.

The second call, max(leave_balance.values()), looks only at the numbers and returns 12, the highest balance.

The third call is the one I actually use. The key parameter tells max() to use leave_balance.get to look up each key’s value before comparing, so it returns the key (“Liam”) tied to the highest balance. For a deeper look at this pattern, see this guide on finding the max value in a dictionary in Python.

The key Parameter: Comparing by a Specific Field

This is where max() becomes genuinely powerful, and it’s the part I use most in my leave app. The key parameter accepts a function. Instead of comparing raw items, max() runs each item through that function, compares the results, and returns the original item with the highest result.

Let’s say each leave request is stored as a dictionary with an employee name and number of days:

requests = [
    {"employee": "Emma", "days": 5},
    {"employee": "Liam", "days": 12},
    {"employee": "Austin", "days": 3},
]

longest_request = max(requests, key=lambda r: r["days"])
print(longest_request)

Output:

{'employee': 'Liam', 'days': 12}

I executed the above example code and added the screenshot below.

Find the Maximum Value in Using max() in Python

I used a lambda, a small, unnamed function you define inline. Here, lambda r: r["days"] tells max() to look at the "days" field of each dictionary r when deciding which one is largest. If lambdas feel unfamiliar, this guide on how to use lambda functions in Python covers the basics.

You can also write this with a named function instead of a lambda:

def get_days(request):
    return request["days"]

longest_request = max(requests, key=get_days)
print(longest_request)

Output:

{'employee': 'Liam', 'days': 12}

Same result. Lambdas are just a shortcut for short, one-time functions.

Getting the Field, Not the Whole Record

Sometimes I only want the employee’s name, not the whole dictionary:

longest_request = max(requests, key=lambda r: r["days"])
print(f"{longest_request['employee']} requested the most days: {longest_request['days']}")

Output:

Liam requested the most days: 12

This pattern — find the max record, then pull the field you need — comes up constantly with multi-attribute data.

Handling Ties and Empty Lists

Two edge cases catch beginners off guard: ties and empty data.

Ties

If two items tie for the maximum, max() returns only the first one it encounters.

tied_requests = [
    {"employee": "Emma", "days": 10},
    {"employee": "Liam", "days": 10},
]

result = max(tied_requests, key=lambda r: r["days"])
print(result)

Output:

{'employee': 'Emma', 'days': 10}

Emma appears first, so she’s returned, even though Liam requested an equal number of days. For every tied record, filter manually instead of relying on max() alone.

The default Parameter for Empty Iterables

Calling max() on an empty list without a fallback raises a ValueError, which matters since your leave-request list might be empty when the app first starts.

new_team_requests = []
print(max(new_team_requests, default=0))

Output:

0

The default parameter tells max() what to return instead of crashing, which I use whenever a new employee has no leave history yet.

Building the Leave Request App: Where max() Fits In

Let’s look at the app I built, since this is where these pieces come together. I kept it as a single file at first, leave_app.py, and only split it into modules (db.py, app.py, reports.py) once it grew past a few hundred lines. For a 10-person team, one file is enough.

Setting Up the Database

I used sqlite3, part of Python’s standard library, which stores an entire database in one local file with no server needed:

import sqlite3

conn = sqlite3.connect("leave_tracker.db")
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS leave_requests (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    employee_name TEXT NOT NULL,
    start_date TEXT NOT NULL,
    end_date TEXT NOT NULL,
    days_requested INTEGER NOT NULL,
    status TEXT DEFAULT 'pending'
)
""")
conn.commit()
print("Table ready.")

Output:

Table ready.

Each row holds one leave request: who asked, the dates, the day count, and its status.

Adding a Leave Request With User Input

Next, I needed a way to add requests from the terminal, using input() to collect the dates as text and datetime to calculate the duration.

from datetime import datetime

def add_leave_request(cursor, conn):
    employee_name = input("Enter employee name: ")
    start_date_str = input("Enter start date (YYYY-MM-DD): ")
    end_date_str = input("Enter end date (YYYY-MM-DD): ")

    start = datetime.strptime(start_date_str, "%Y-%m-%d")
    end = datetime.strptime(end_date_str, "%Y-%m-%d")
    days_requested = (end - start).days

    cursor.execute(
        "INSERT INTO leave_requests (employee_name, start_date, end_date, days_requested) VALUES (?, ?, ?, ?)",
        (employee_name, start_date_str, end_date_str, days_requested)
    )
    conn.commit()
    print(f"Added request for {employee_name}: {days_requested} days")

Output (sample run):

Enter employee name: Emma Johnson
Enter start date (YYYY-MM-DD): 2026-08-03
Enter end date (YYYY-MM-DD): 2026-08-07
Added request for Emma Johnson: 4 days

I use strptime() to turn typed text into a real datetime object, since you can’t subtract plain strings but you can subtract datetime objects. end - start gives a timedelta, and .days