"Unpacking and Packing" in Python

"Unpacking and Packing" in Python

ยท

3 min read

Since I struggled a lot to understand this concept when I started with Python (being from a non-CS non-coding background), I decided to share my knowledge in layman terms hoping to benefit others struggling as well :)

Let's Get Started !!

UNPACKING

What do you understand from the term "Unpacking"???

That's right : "to open and remove contents of something". Unpacking in python is somewhat similar to the English term.

Let's take an example :

image.png

What's happening here?

We have a list on the right side of = operator . It has 3 "contents" inside it : 1,2 and 3. The "contents" from the box (list) are "unpacked" and assigned to the variables on the left side (a,b,c) with respect to their positions. First position "a" gets the first position element 1. Second position "b", gets the second position element 2 and so on.....

Rule :

  1. The rule is to have as many variables on the left of the = operator as there are elements to be assigned on the right . As you can see in the above example, 3 elements (1,2,3) and 3 variables (a,b,c).

  2. Variables like (a,b,c) mentioned above are mandatory variables. Something HAS to be assigned to them otherwise you'll encounter an error.

"Unpacking Operator "

The next example is the one which got me so mad confused between the 2 concepts of Packing and Unpacking. I was like : Dude is this "Packing" coz contents are being put together inside a single variable or is this "Unpacking" coz contents are being unpacked from an iterable (list,tuple) first????? (Thank God for Internet :P )

image.png

This example is actually a case of "Unpacking as well as Packing".

Notice that we have a variable "a" with an asterisk (*) before it . The asterisk is known as "Unpacking Operator". The asterisk before "a" tells the system that "All the elements are to be assigned to "a" apart from the ones reserved for mandatory variables b and c. "

The "Unpacking" occurs for the variables b and c wherein the elements 4 and 5 are unpacked from the tuple and assigned to them respectively. Whereas , we can say that for variable "a", the elements 1,2,3 are unpacked from the tuple and then "packed" together to be assigned collectively to "a".

Look at this example from the above snippet for further understanding :

*a, = (1,2,3)

If you'll notice, there is a comma I've placed after "a". What is it for?

In the absence of that comma, you'll fetch an error stating that the elements to be assigned are more than the variables (Rule 1 stated above). A comma tells the machine that "a" is actually a tuple and (*) informs that "a" is a tuple ready to take in as many elements as you wish to enter.

As I told you, notice the comma after "a" and the asterisk before it, informing the system "no need to worry sir, "a" is a tuple, bring in as many elements as you wish to (smirk!!) " . So, all the elements are "packed" together and assigned to "a".

This is how "Unpacking" and "Packing" in python work on a basic level. Play with it and learn more.

I will explain further on this topic involving (*Args) and (**Kwargs) in the next Blog. Stay Tuned!!!!!

Contact me on my twitter account for any queries : @TaNiSi119

Did you find this article valuable?

Support Sonakshi Tyagi by becoming a sponsor. Any amount is appreciated!

ย