Python Simplified

PythonSimplifiedcomLogo

What is Unpacking in Python

What is Unpacking in Python resized

Introduction

For many years, unpacking was referred to as tuple unpacking even though it is applicable to other iterables such as list, tuple, string, range, etc. Even today, many still think unpacking is related to tuples.

So the goal of this article is to go through what is unpacking in Python, different ways of unpacking. Along the way, you will understand that not only tuples but list, sets, dictionaries etc. can also be used for unpacking. 

Unpacking

The process of splitting the packed values into individual elements is called ‘unpacking’. The packed values are strings, lists, tuples, sets, and dictionaries.

 

During the unpacking elements from RHS (Right Hand Side) are split into its relative position on the LHS (Left Hand Side). We will see how this works in the following examples.

 

As we can see from the below two examples, elements from RHS are unpacked into the variables on LHS based on the relative position.

Example 1: Using list, tuple & string

Example 2: Using sets and dictionaries

Since sets & dictionaries are unordered collection of elements it is not guaranteed that unpacking will give the same results every time unlike strings, lists, and tuples which are an ordered collection of elements. Note that for dictionary type, unpacking is done on keys.

Tip: Since sets & dictionaries are an unordered collection of elements, it is not recommended to use them for unpacking as there is no guarantee on the order of the results.

Extended iterable unpacking

The extended iterable unpacking is done via the operators * and **.

* Operator

Sometimes we don’t want to unpack all the elements. We might be interested in unpacking only the first element or the last element and put rest of the elements into a variable. This can be achieved with the help of * operator.

Note: * operator can only be used once on the LHS of assignment operator unless it is used in nested unpacking.

Example 3: Using list, tuple, string

Example 4: Using sets and dictionaries

** Operator

As you have noticed from the above examples, * operator iterates through the keys in the dictionary. If we would like to iterate through key-value pairs, then we must use ** operator.

 

Note: ** operator can not be used on the LHS of assignment operator.

Example 5: Using dictionaries

As you can see from the below example, all the key-value pairs are used for unpacking using ** operator.

In this example, the ‘k4’ appears both in d2 and d3. In such cases, the latest key and its value is considered for the final results after unpacking.

Nested unpacking

The same concepts we just went through for unpacking and extended unpacking applies to nested unpacking as well. The only difference is as the name suggests — it’s nested. Let’s look at the examples:

Example 6: Using list, tuple & string

Conclusion

In this article, you have understood about unpacking, extended iterable unpacking and nested unpacking with examples. If you have any questions, please let me know in the comments section and I will try to get back you as soon as I can. 

References

Originally published at Medium on 5-Sep-2020.

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on email
Chetan Ambi

Chetan Ambi

A Software Engineer & Team Lead with over 10+ years of IT experience, a Technical Blogger with a passion for cutting edge technology. Currently working in the field of Python, Machine Learning & Data Science. Chetan Ambi holds a Bachelor of Engineering Degree in Computer Science.
Scroll to Top