TNS
VOXPOP
AI-Aided Coding?
On average, how much time do you think you save per week by an AI-powered coding assistant such as GitHub Copilot or JetBrains AI Assistant?
✓
I don’t use an AI coding assistant.
0%
✓
Up to 1 hour/week
0%
✓
1-3 hours
0%
✓
3-5 hours
0%
✓
5-8 hours
0%
✓
More than 8 hours
0%
✓
I don’t save any time, yet!
0%
Python

Packing and Unpacking in Python

By using packing and unpacking you can create assignments with a single statement and catch several values with a single identifier, making your code much easier to read.
Feb 28th, 2024 5:00pm by
Featued image for: Packing and Unpacking in Python
Feature image by Mohamed Hassan from Pixabay.

Packing is a handy Python tool that provides a dynamic way to pack and unpack values into a single data structure or take them out and assign them to multiple variables. This process greatly improves the reliability and adaptability of your code.

In Python, “packing” refers to the process of putting values into a new tuple or list by separating them with commas. This can be done with an assignment statement where the left-hand side is a tuple or list of variables and the right-hand side is another tuple or list of values.

For example, you might have three variables, which are number1, number2 and number3, which are defined as such:


You can pack those variables into a tuple like so:


Or a list like so:


You can also use the * operator (which is used as both power and multiplication operators). Before we see how that works with packing, let me show you how the * operator works. We’ll define a function called sumOf and use the * operator like so:


 

Know that *nums stores the series of numbers and then the sum() function adds them together. So, when run, the output of the above would be:


Back to packing, we can use the * operator with packing like so:


But how do we use packing in actual Python code?

Packing with Tuples

Remember, a tuple is an immutable list, so it cannot be changed once created. Tuples are great options for storing data that you don’t want to be modified. Let’s say we want to create a simple program, using tuples and packing to sum up a string of numbers. We’ll define our custom function as such:


Above, we’re sending a dynamic number of arguments to the addPack function and are accessing them via a single myTuple parameter by using the * operator.

Next, we pack our arguments with the following two lines:


Because we’re using dynamic arguments for packing, we could have continued with our numbers like so:


When we run the application, it will add the numbers in both tuples and total them together. What this has done is pack the addPack function with variables from the print functions.

You can also collect multiple values with packing and use certain parts in different ways. Take,  for instance, this piece of Python code:


The above performs what is called a string split to collect only the first name of someone with multiple names. What this does is collect the first name in one variable and the remaining names into another (hence the string split).

The output of the above packing example would be:

Unpacking with Tuples

In Python, unpacking is the process of extracting values from a sequence and assigning them to multiple variables. We achieve unpacking with the assignment operator on the left side and an iterable object on the left side.

For example, we can unpack with a tuple like so:


The output of the above would be:


Another example of unpacking a tuple looks like this:


What we’ve done is assign values to multiple variables and then unpack them. The output of the above script would be:


We can unpack a list like this:


The output of the above would be:


We can also unpack dictionaries (an unordered collection of data in the format of a key-value pair) like so:


The output of the above script would be:


Unpacking allows you to assign values from a sequence to multiple variables, using only a single line of code.

By using packing and unpacking you can create assignments with a single statement and catch several values with a single identifier, thereby making your code much easier to read. These two features allow for much more flexible handling of function arguments and streamlined data manipulation.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.