Title: Unpacking Function Arguments
Slug: unpacking_function_arguments
Summary: Unpacking Function Arguments in Python.
Date: 2016-01-23 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Interesting in learning more? Check out Fluent Python

Create Argument Objects


In [1]:
# Create a dictionary of arguments
argument_dict = {'a':'Alpha', 'b':'Bravo'}

# Create a list of arguments
argument_list = ['Alpha', 'Bravo']

Create A Simple Function


In [2]:
# Create a function that takes two inputs
def simple_function(a, b):
    # and prints them combined
    return a + b

Run the Function With Unpacked Arguments


In [3]:
# Run the function with the unpacked argument dictionary
simple_function(**argument_dict)


Out[3]:
'AlphaBravo'

In [4]:
# Run the function with the unpacked argument list
simple_function(*argument_list)


Out[4]:
'AlphaBravo'