We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Python is the most used programming language.

Unpacking box image

Today youll learn to use one of its core but often ignored features, unpacking in Python.

Well be walking through the concept of unpacking, and how to use it to write more Pythonic code.

If you dont have it installed check out our Python installation guide.

When you see this, it means were using the arithmetic operators.

So go ahead and read the entire tutorial!

Whats unpacking?

Unpacking is the process of getting out stuff iterables such as lists, tuples, and dictionaries.

This kind of variable assignment is the fundamental concept of unpacking in Python.

Its probably you expected this kind of error.

Essentially were assigning 4 list items to three variables, how does Python manage to assign the right values?

It doesnt, thats because we get aValueErrorwith the message too many values to unpack.

Sure there is, and its called unpacking operator or asterisk operator (*, **).

Lets see how to use it in Python.

Common troubleshooting

We can unpack a unique element of an iterable.

Unfortunately, we cant unpack a dictionary to a single variable as weve been doing with tuples and lists.

Lets see why do we need to use them along with callables.

Itll certainly raise an error because the function is receiving more arguments than its capable of manage.

Finally, lets get the object throw in of theargsof a function.

This means that with this operator were able to pass key-value pairs to the function as a parameter.

Lets create a functionmake_person, which receives a positional argument name, and an undefined amount of keyworded arguments.