Python – Packing and Unpacking Dictionaries

Today I will explain the concept of unpacking and packing within Python.


Unpacking allows us to pass keyword arguments (i.e dictionary) to a function via the use of the ** syntax. We can then access the values within the function like so,

>>> def do_something(**kwargs):
...     print kwargs['a']
...     print kwargs['b']
...     print kwargs['c']


Packing allows you to pass a set of keyword values to a function, i.e like a dictionary. This is far quicker then having to explicitly pass each keyword. An example is shown below,

// with packing
>>> some_values = {'b': 2, 'c': 3}
>>> do_something(a=1, **some_values)

// without packing.
>> do_something(a=1, b=2, c=3)
Rick Donato

Want to become a programming expert?

Here is our hand-picked selection of the best courses you can find online:
Python Zero to Hero course
Python Pro Bootcamp
Bash Scripting and Shell Programming course
Automate with Shell Scripting course
The Complete Web Development Bootcamp course
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial