# 4.11 Arbitrary Argument Lists¶

- Functions with arbitrary argument lists, such as built-in functions `min` and `max`, can receive any number of arguments.
- Function `min`'s documentation states that `min` has two required parameters (named `arg1` and `arg2`) and an optional third parameter of the form `*args`, indicating that the function can receive any number of additional arguments.
- The `*` before the parameter name tells Python to pack any remaining arguments into a tuple that’s passed to the `args` parameter.

### Defining a Function with an Arbitrary Argument List¶

- `average` function that can receive any number of arguments.

In [1]:

    def average(*args):
        return sum(args) / len(args)

- The `*args` parameter must be the rightmost parameter.

In [2]:

    average(5, 10)

Out[2]:

    7.5

In [3]:

    average(5, 10, 15)

Out[3]:

    10.0

In [4]:

    average(5, 10, 15, 20)

Out[4]:

    12.5

### Passing an Iterable’s Individual Elements as Function Arguments¶

- Can unpack a tuple’s, list’s or other iterable’s elements to pass them as individual function arguments.
- The `*` operator, when applied to an iterable argument in a function call, unpacks its elements.

In [5]:

    grades = [88, 75, 96, 55, 83]

In [6]:

    average(*grades)

Out[6]:

    79.4

- Equivalent to `average(88, 75, 96, 55, 83)`.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 4 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.