Short answer: Use a star to collect the arguments in a tuple and then add a special case for a tuple of length one to handle a single iterable argument.

Source material: The C code that handles the logic can be found at: https://github.com/python/cpython/blob/da20d7401de97b425897d3069f71f77b039eb16f/Python/bltinmodule.c#L1708

Simplified pure python code: If you ignore the default and key keyword arguments, what's left simplifies to:

def mymax(*args):
    if len(args) == 0:
        raise TypeError('max expected at least 1 argument, got 0')
    if len(args) == 1:
        args = tuple(args[0])
    largest = args[0]
    for x in args[1:]:
        if x > largest:
            largest = x
    return largest

There are other nuances, but this should get you started.

Documentation: The special handling for the length one case versus other cases is documented here:

Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

More complete version: This includes some of aforementioned nuances like the key and default keyword arguments and the use of iterators instead of slices:

sentinel = object()

def mymax(*args, default=sentinel, key=None):
    """max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.
    """
    if not args:
        raise TypeError('max expected at least 1 argument, got 0') 
    if len(args) == 1:
        it = iter(args[0])
    else:
        if default is not sentinel:
            raise TypeError('Cannot specify a default for max() with multiple positional arguments')
        it = iter(args)
    largest = next(it, sentinel)
    if largest is sentinel:
        if default is not sentinel:
            return default
        raise ValueError('max() arg is an empty sequence')
    if key is None:
        for x in it:
            if x > largest:
                largest = x
        return largest
    largest_key = key(largest)
    for x in it:
        kx = key(x)
        if kx > largest_key:
            largest = x
            largest_key = kx
    return largest

# This makes the tooltips nicer
# but isn't how the C code actually works
# and it is only half correct.
mymax.__text_signature__ = '($iterable, /, *, default=obj, key=func)'
Answer from Raymond Hettinger on Stack Overflow
🌐
Real Python
realpython.com › ref › builtin-functions › max
max() | Python’s Built-in Functions – Real Python
This example demonstrates how max() can help you identify the most expensive products by using the dictionary’s values for comparison. You can support max() in custom classes by implementing the .__gt__() special method.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-max-function
Python - max() function - GeeksforGeeks
July 15, 2025 - By default, it will return the string with the maximum lexicographic value. In this example, as max() is used to locate the largest item in Python, we are using max() to find maximum out of 3 string variable.
Discussions

Implementation of the max() function in Python - Stack Overflow
Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools. Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content. Stack Ads Connect your brand to the world’s most trusted technologist communities. Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... The 2026 Annual Developer Survey is live— take the Survey today! pythonjavascriptc... More on stackoverflow.com
🌐 stackoverflow.com
Creating a max function from scratch (python) - Stack Overflow
So I'm trying to create a function that determines the maximum of a set of values, from scratch. I'm not sure how to make the function change the new value of Max after a new Max has been found, ... More on stackoverflow.com
🌐 stackoverflow.com
Min-max python v3 implementation - Stack Overflow
I'm currently trying to code an equivalent for the built-in min-max function in python, and my code return a pretty weird exception which I don't understand at all: TypeError: 'generator' object... More on stackoverflow.com
🌐 stackoverflow.com
Ping in Python
Generally speaking, a pure-Python ICMP implementation is unnecessarily complicated. subprocess.check_call(['ping','-c1',ip]) More on reddit.com
🌐 r/Python
15
34
April 17, 2014
People also ask

What is the use of the max() and min() functions in Python?
We use Python's max() and min() functions to get the maximum or minimum object out of a set of objects or max/min object out of objects in a given iterable.
🌐
interviewkickstart.com
interviewkickstart.com › home › blogs › learn › max() function in python: syntax, parameters, and examples
max() Function in Python: Syntax, Parameters & Examples
What is the difference between the Python max() and min() functions?
Both compare items or items within an iterable on some basis and return a value. But the max() function in Python returns the max value after those comparisons, and the min() function returns the minimum value.
🌐
interviewkickstart.com
interviewkickstart.com › home › blogs › learn › max() function in python: syntax, parameters, and examples
max() Function in Python: Syntax, Parameters & Examples
🌐
Unstop
unstop.com › home › blog › python max() function | all use cases with code examples
Python max() Function | All Use Cases With Code Examples
February 12, 2025 - In the example Python program, the max() function compares all the elements in the list iterable object and returns 19 as the largest value. When applied to strings, max() compares them lexicographically (alphabetically).
🌐
GitHub
gist.github.com › yvan-sraka › 52384523a92bb0910d770f0fdde59bbd
Max function implementation explained in Python - Gist - GitHub
To correct this you should change line 9 to: plus_grand_nombre = liste[0] ... def max(list): if list[1:]: if list[0] > max(list[1:]): return list[0] else: return max(list[1:]) elif not list: return else: return list[0]
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › max() function in python: syntax, parameters, and examples
max() Function in Python: Syntax, Parameters & Examples
April 1, 2026 - word1 = "apple" word2 = "banana" word3 = "apricot" print(max(word1, word2, word3)) # Output: 'banana' The key parameter changes what Python compares. In this example, max() returns the longest string instead of the string that comes last ...
Top answer
1 of 1
46

Short answer: Use a star to collect the arguments in a tuple and then add a special case for a tuple of length one to handle a single iterable argument.

Source material: The C code that handles the logic can be found at: https://github.com/python/cpython/blob/da20d7401de97b425897d3069f71f77b039eb16f/Python/bltinmodule.c#L1708

Simplified pure python code: If you ignore the default and key keyword arguments, what's left simplifies to:

def mymax(*args):
    if len(args) == 0:
        raise TypeError('max expected at least 1 argument, got 0')
    if len(args) == 1:
        args = tuple(args[0])
    largest = args[0]
    for x in args[1:]:
        if x > largest:
            largest = x
    return largest

There are other nuances, but this should get you started.

Documentation: The special handling for the length one case versus other cases is documented here:

Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

More complete version: This includes some of aforementioned nuances like the key and default keyword arguments and the use of iterators instead of slices:

sentinel = object()

def mymax(*args, default=sentinel, key=None):
    """max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.
    """
    if not args:
        raise TypeError('max expected at least 1 argument, got 0') 
    if len(args) == 1:
        it = iter(args[0])
    else:
        if default is not sentinel:
            raise TypeError('Cannot specify a default for max() with multiple positional arguments')
        it = iter(args)
    largest = next(it, sentinel)
    if largest is sentinel:
        if default is not sentinel:
            return default
        raise ValueError('max() arg is an empty sequence')
    if key is None:
        for x in it:
            if x > largest:
                largest = x
        return largest
    largest_key = key(largest)
    for x in it:
        kx = key(x)
        if kx > largest_key:
            largest = x
            largest_key = kx
    return largest

# This makes the tooltips nicer
# but isn't how the C code actually works
# and it is only half correct.
mymax.__text_signature__ = '($iterable, /, *, default=obj, key=func)'
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_func_max.asp
Python max() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The max() function returns the item with the highest value, or the item with the highest value in an iterable.
🌐
Programiz
programiz.com › python-programming › methods › built-in › max
Python max()
# find max among the arguments result = max(4, -5, 23, 5) print("The maximum number is:", result) ... If you need to find the smallest item, you can use the Python min() function.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › max function in python
Max Function in Python | Examples of Max Function in Python
March 28, 2023 - ... # Program to illustrate max() functionality # example 1: Input --> basic integers print("input --> 1, 9, 72, 81, 28, 49, 90, 102, 150, 190") print("max: ", max(1, 9, 72, 81, 28, 49, 90, 102, 150, 190)) # Input --> a string print("input ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
ZetCode
zetcode.com › python › max-builtin
Python max Function - Complete Guide
April 11, 2025 - The first example finds the longest word using len as the key. The second calculates the sum of ASCII values for each character in the word. Key functions allow flexible comparisons without modifying the original data or creating custom comparison methods. You can make custom objects work with ...
🌐
Devcuriosity
devcuriosity.com › manual › details › python-max-function
Python - max() function with examples - DevCuriosity
In this example, with function ord() we can see that letter 'w' has the biggest value - 119. my_list = ["www", "devcuriosity", "com"] my_max = max(my_list, key=len) print(my_max) # devcuriosity
🌐
Hyperskill
hyperskill.org › university › python › max-in-python
Python max() Function — Syntax, key Parameter & Examples
August 10, 2026 - This course is dedicated to core Python skills that will give you a solid base and allow you to pursue any further direction, be it Backend Development or Data Science. ... The max() function in Python is commonly employed to identify the value from a provided collection of elements.
🌐
ThePythonGuru
thepythonguru.com › python-builtin-functions › max
Python max() function - ThePythonGuru.com
January 7, 2020 - # find largest item in the string print(max("abcDEF")) # find largest item in the list print(max([2, 1, 4, 3])) # find largest item in the tuple print(max(("one", "two", "three"))) 'two' # find largest item in the dict print(max({1: "one", 2: "two", 3: "three"})) 3 # empty iterable causes ...
🌐
Wikibooks
en.wikibooks.org › wiki › Algorithms › Find_maximum › Python_method_1
Algorithms/Find maximum/Python method 1 - Wikibooks, open books for an open world
The code needs to be reviewed. def findmax(a): if len(a) == 0: return 0 curr_max = a[0] for i in a: if i > curr_max: curr_max = i return curr_max · Simple example of how to use the function: print(findmax([12, 13, 555, 124, 342])) Output should be: 555 · Another way to implement the algorithm: ...
🌐
Learn By Example
learnbyexample.org › python-max-function
Python max() Function - Learn By Example
April 20, 2020 - x = max('red', 'green', 'blue') print(x) # Prints red
🌐
AskPython
askpython.com › python › examples › max-heap
Max Heap Data Structure - Complete Implementation in Python - AskPython
March 17, 2021 - Now, we will implement a max-heap in Python. We use a list [15, 7, 9, 4, 13] in the code and convert it to a max heap using the build-heap function.
🌐
Tutorialspoint
tutorialspoint.com › python › number_max.htm
Python max() Function
# Creating a list List = [74,587,24,92,4,2,7,46] res = max(List) print("The largest number in the list is: ", res) While executing the above code we get the following output − ... In the example below, a list of equal length of strings is created.
Top answer
1 of 3
3

Here is my implementation:

def max(*args, **kwargs):
    key = kwargs.get("key", lambda x: x)
    if len(args) == 1:
        args = args[0]
    maxi = None
    for i in args:
        if maxi == None or key(i) > key(maxi):
            maxi = i
    return maxi

def min(*args, **kwargs):
    key = kwargs.get("key", lambda x: x)
    if len(args) == 1:
        args = args[0]
    mini = None
    for i in args:
        if mini == None or key(i) < key(mini):
            mini = i
    return mini

A little bit more concise than preview post.

2 of 3
2

The issue you are having is due to the fact that min has two function signatures. From its docstring:

min(...)
    min(iterable[, key=func]) -> value
    min(a, b, c, ...[, key=func]) -> value

So, it will accept either a single positional argument (an iterable, who's values you need to compare) or several positional arguments which are the values themselves. I think you need to test which mode you're in at the start of your function. It is pretty easy to turn the one argument version into the multiple argument version simply by doing args = args[0].

Here's my attempt to implement the function. key is a keyword-only argument, since it appears after *args.

def min(*args, key=None):    # args is a tuple of the positional arguments initially
    if len(args) == 1:       # if there's just one, assume it's an iterable of values
        args = args[0]       # replace args with the iterable

    it = iter(args)          # get an iterator

    try:
        min_val = next(it)   # take the first value from the iterator
    except StopIteration:
        raise ValueError("min() called with no values")

    if key is None:       # separate loops for key=None and otherwise, for efficiency
        for val in it:    # loop on the iterator, which has already yielded one value
            if val < min_val
                min_val = val
    else:
        min_keyval = key(min_val)    # initialize the minimum keyval
        for val in it:
            keyval = key(val)
            if keyval < min_keyval:  # compare keyvals, rather than regular values
                min_val = val
                min_keyval = keyval

    return min_val

Here's some testing:

>>> min([4, 5, 3, 2])
2
>>> min([1, 4, 5, 3, 2])
1
>>> min(4, 5, 3, 2)
2
>>> min(4, 5, 3, 2, 1)
1
>>> min(4, 5, 3, 2, key=lambda x: -x)
5
>>> min(4, -5, 3, -2, key=abs)
-2
>>> min(abs(i) for i in range(-10, 10))
0