Skip to content

How to Apply Functions with Multiple Arguments in Python

Learn how to pass positional, keyword, and default arguments in Python, plus advanced techniques like *args, **kwargs, argument unpacking, and partial application for flexible function usage.

In Python, functions are designed to be flexible. Whether you are passing data by position, by keyword, or unpacking lists and dictionaries into arguments, understanding how to apply functions with multiple arguments is essential for writing clean, modular code.

This guide explores the mechanics of passing arguments, the power of *args and **kwargs, and advanced techniques like argument unpacking and partial application.

Basic Argument Passing Mechanisms

Python offers three fundamental ways to pass arguments to a function: positional, keyword, and default.

  • Positional: Based on order.
  • Keyword: Based on parameter name (order irrelevant).
  • Default: Optional parameters that have a preset value.
def create_profile(name, age, city="Unknown"):
    print(f"Name: {name}, Age: {age}, City: {city}")

create_profile("Alice", 30, "Paris")

create_profile(age=25, name="Bob")

create_profile("Charlie", 40) 

Output:

Name: Alice, Age: 30, City: Paris
Name: Bob, Age: 25, City: Unknown
Name: Charlie, Age: 40, City: Unknown

Variable Arguments: *args and **kwargs

To create functions that accept a dynamic number of arguments, Python uses the * (asterisk) and ** (double asterisk) syntax in function definitions.

  • *args: Collects extra positional arguments into a tuple.
  • **kwargs: Collects extra keyword arguments into a dictionary.
def dynamic_function(*args, **kwargs):
    print(f"Positional args (tuple): {args}")
    print(f"Keyword args (dict):     {kwargs}")

dynamic_function(1, 2, 3, status="active", role="admin")

Output:

Positional args (tuple): (1, 2, 3)
Keyword args (dict):     {'status': 'active', 'role': 'admin'}

Argument Unpacking (Applying Collections)

A common scenario is having data inside a list or dictionary and needing to pass it to a function that expects separate arguments. This is often called "unpacking."

Unpacking Lists/Tuples with *

def multiply(x, y, z):
    return x * y * z

numbers = [2, 3, 4]

try:
    print(multiply(numbers))
except TypeError as e:
    print(f"Error: {e}")

print(f"Result: {multiply(*numbers)}")

Output:

Error: multiply() missing 2 required positional arguments: 'y' and 'z'
Result: 24

Unpacking Dictionaries with **

If your dictionary keys match the function parameter names, you can unpack them directly.

def configure(host, port, debug=False):
    print(f"Connecting to {host}:{port} (Debug: {debug})")

config_data = {
    "host": "localhost",
    "port": 8080,
    "debug": True
}

configure(**config_data)

Output:

Connecting to localhost:8080 (Debug: True)

Partial Application with functools

Sometimes you want to "freeze" certain arguments of a function to create a new, simplified version of that function. This is known as partial application.

from functools import partial

def power(base, exponent):
    return base ** exponent

square = partial(power, exponent=2)

cube = partial(power, exponent=3)

print(f"4 squared is: {square(4)}")
print(f"2 cubed is:   {cube(2)}")

Output:

4 squared is: 16
2 cubed is:   8

Conclusion

Mastering argument application in Python allows for flexible and reusable code.

  1. Use *args and **kwargs in definitions to accept dynamic inputs.
  2. Use *list to unpack sequences into positional arguments.
  3. Use **dict to unpack dictionaries into keyword arguments.
  4. Use functools.partial to pre-fill arguments and create specialized functions.
Navigation