Unpack Python Items

Image for post
Image for post
Photo by Markus Spiske on Unsplash

For a person like me who started from JavaScript, Pack/Unpack in Python is not really a new concept, thinking about Destructuring and ES6 Spread/Rest Operator as a comparison. I’m pretty sure similar features are available in other major languages. Still, I’d like to have an overview of the Unpacking operation in Python with concrete examples — as it can be a bit bewildering for people who learn it for the first time.

So what is unpacking?

It refers to an operation that assigning an iterable value to a collection data types (e.g. tuple and dictionary). In comparison, packing is when assigning an iterable value into a single variable.

Note that when we say a collection data types we refer to both sequence and iterables. A sequence is like an ordered collection of iterables such as List, Dictionary, Tuples, Strings.

See examples below for unpacking in various scenarios:

Tuple:
>>> x, y, z= 1, 2, 3
>>> x
1
>>> y
2
>>> z
3

The above is unpacking using a single variable. The next step is unpacking using known number of values or unknown number of values.

Known Number of Values, we need to have exact number of variables to hold the number of elements in the iterable to be unpacked:

var = [1, 2, 3]
x, y, z= var

For unknown number of variables, we can use * or ** depends on if the iterable is a dictionary or not. Note that to unpack multiple values into a single variable, we need to add a comma after the * to indicate it’s a tuple/list.

The * operator for packing tuple/list/set/string/generator kind of iterable.

>>> *x, y = 1, 2, 3
>>> x
[1, 2]
>>> y
3

We can also use * to spread the packed iterable into another iterable (tuple/set/list).

Tuple
>>> var = (1, 2, 3)
>>> (0, *var, 4)
(0, 1, 2, 3, 4)

For unpacking a dictionary type, we use **.

>>> var_dict = {'a': 1, 'b':2, 'c': 3}
>>> final = {**var_dict}
>>> final
{'a': 1, 'b':2, 'c': 3}

Note that when you unpack multiple dictionaries into one. The right side of the variable will overwrite the left side of variable given there are same keys:

>>> var_dict = {'a': 1, 'b':2, 'c': 3}
>>> dict_var = {"a": 4, "b":5, "d": 6}
>>> {**var_dict, **dict_var}
{'a': 4, 'b': 5, 'c': 3, 'd': 6}

Yet the most common usage of * and **, as you might have seen it already, is to define functions arguments:

>>> def fruit(any, *args, **kwargs):
print(any, *args, **kwargs)

As you can see apple and pear are positional arguments (), and `another='mango is keyword arguments {} that are read the value mango in the printing command.

We can also pack multiple variables in the function call (instead of function definition), note that the keyword argument has to match the key of the passed-in parameter.

>>> def fruit(any, args, another):
print(any, args, another)

Combine together:

>>> def fruit(any, *args, **kwargs):
print(any, args, kwargs)

That’s pretty much it!

Happy Reading!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store