Packing is a technique in python with which we put several values into a single iterator. If we talk of packing in literal terms, Just like we pack certain items into a box in the real world, In python we pack certain variables in a single iterable.  In this article, we will study how to perform packing and use it in our programs.

How to perform packing in Python?

We can perform packing by using simple syntax for declaration of iterables like list or tuples or we can use  asterisk operator * for packing. The * operator is used as an operator for unpacking the iterables into variables but it allows us to pack a number of variables into a single variable.

For example, suppose we have three  variables num1, num2,num3. We can pack them into a tuple as follows.

num1=1
num2=2
num3=3
myTuple=(num1,num2,num3)

Or we can pack them into a list as follows.

num1=1
num2=2
num3=3
myList=[num1,num2,num3]

Alternatively, we can use * operator to pack the numbers together as follows.

num1=1
num2=2
num3=3
*num,=num1,num2,num3

We have used a comma “,” after the *num because the left hand side of the assignment operation must be a tuple or list otherwise error will be encountered.

In the left side, when we use * operator, we can also have other variables which can be assigned the values. For example, we can pack two numbers into a variable and a third number can be assigned to another variable as follows.
num1=1
num2=2
num3=3
*num,myNum=num1,num2,num3

In the above example, we have to remember that myNum is a mandatory variable and it must be assigned some value whereas *num can be assigned no value. Here, num3 will be assigned to myNum and num1 and num2 will be packed in the list num.

Using packing in passing arguments

When we don’t know how many arguments will be passed to a function, we use packing to handle the situation. We can declare initial few variables and then we can use the asterisk operator to pack the remaining arguments in a single variable as follows.

def sumOfNumbers(num1,num2,*nums):
    temp=num1+num2
    for i in nums:
        temp=temp+i
    return temp

In the above example, when only two numbers are passed to calculate their sum, nums remains empty and the sum of the two numbers is returned. When we pass more than two numbers to the function, the first and second argument are assigned to num1 and num2 and rest of the numbers in the argument will be packed in nums which behaves as a list. After that the sum of the numbers will be calculated.

Collecting multiple values with packing

While writing programs, there may be situations where we want to split a sequence or string or an iterable into many parts. For example, we may need to perform a python string split operation to collect the first name of a person from a string containing the name. Now, if we don’t know how many words may be in the name of the person, we can collect the first name in a variable and rest of the words in a variable using packing as follows.

name="Joseph Robinette Biden Jr"
first_name, *rest_name=name.split()
print("Full Name:")
print(name)
print("First Name:")
print(first_name)
print("Rest Parts of Name:")
print(rest_name)

Output:

Full Name:
Joseph Robinette Biden Jr
First Name:
Joseph
Rest Parts of Name:
['Robinette', 'Biden', 'Jr']

Merge two iterables using packing in python

We can merge different iterables such as list, tuple,set and dictionary using asterisk operator (*) in python.

For merging two tuples to  make a single tuple, we can use packing.  We can merge two tuples using the asterisk operator as follows.

print("First Tuple is:")
tuple1=(1,2,3)
print(tuple1)
print("Second Tuple is:")
tuple2=(4,5,6)
print(tuple2)
print("Merged tuple is:")
myTuple=(*tuple1,*tuple2)
print(myTuple)

Output:

First Tuple is:
(1, 2, 3)
Second Tuple is:
(4, 5, 6)
Merged tuple is:
(1, 2, 3, 4, 5, 6)

Just like tuples, we can merge two lists as follows.

print("First List is:")
list1=[1,2,3]
print(list1)
print("Second List is:")
list2=[4,5,6]
print(list2)
print("Merged List is:")
myList=[*list1,*list2]
print(myList)

Output:

First List is:
[1, 2, 3]
Second List is:
[4, 5, 6]
Merged List is:
[1, 2, 3, 4, 5, 6]

We can also use packing to merge two or more dictionaries into one python dictionary . For dictionaries, use the dictionary unpacking operator ** to unpack the two initial dictionaries after which they are packed in a third dictionary. This can be understood from the following example.

print("First Dictionary is:")
dict1={1:1,2:4,3:9}
print(dict1)
print("Second Dictionary is:")
dict2={4:16,5:25,6:36}
print(dict2)
print("Merged Dictionary is:")
myDict={**dict1,**dict2}
print(myDict)

Output:

First Dictionary is:
{1: 1, 2: 4, 3: 9}
Second Dictionary is:
{4: 16, 5: 25, 6: 36}
Merged Dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}

Conclusion

In this article, We have studied about packing in python and have implemented different programs to understand the use cases of packing operation using asterisk operator (*). Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *