Python provides us with the packing and unpacking operator to convert one iterable object to another easily. In this article, we will discuss the unpacking operator in Python with different examples.
What is the Unpacking Operator in Python?
The unpacking operator in Python is used to unpack an iterable object into individual elements. It is represented by an asterisk sign *
and has the following syntax.
*iterable_object
Here,
- The
iterable_object
variable represents an iterable object such as a list, tuple, set, or a Python dictionary. - After execution of the above statement, the elements of
iterable_object
are unpacked. Then, we can use the packing operation to create other iterable objects.
To understand this, consider the following example.
myList=[1,2,3,3,4,4,5,6]
mySet={*myList}
print("The list is:")
print(myList)
print("The set is:")
print(mySet)
Output:
The list is:
[1, 2, 3, 3, 4, 4, 5, 6]
The set is:
{1, 2, 3, 4, 5, 6}
In the above example, the *
operator unpacks myList
. Then, we created a set from the unpacked elements.
Remember that you cannot use the unpacking operator to assign the elements of the iterable object to individual elements. If you do so, the program will run into an error. You can observe this in the following example.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
a,b,c,d,e,f=*myList
Output:
The list is:
[1, 2, 3, 4, 5, 6]
File "/tmp/ipykernel_16212/1381937598.py", line 4
a,b,c,d,e,f=*myList
^
SyntaxError: can't use starred expression here
In this example, we have tried to assign elements from myList
to six variables using the *
operator. Hence, the program runs into SyntaxError exception.
Unpacking in Python Using Parallel Assignment
Instead of using the *
operator, you can unpack an iterable object into multiple variables using parallel assignment. For this, you can use the following syntax.
var1, var2, var3,var4..varN=iterable_object
In the above statement, variables var1
, var2
, var3
,var4
till varN
are individual variables. After execution of the statement, all the variables are initialized with elements from iterable_object
as shown below.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
a,b,c,d,e,f=myList
print("The variables are:")
print(a,b,c,d,e,f)
Output:
The list is:
[1, 2, 3, 4, 5, 6]
The variables are:
1 2 3 4 5 6
In the above example, we have six elements in myList
. Hence, we have unpacked the list into six variables a
, b
, c
, d
, e
, and f
.
In the above syntax, the number of variables must be equal to the number of elements in the iterable_object
. Otherwise, the program will run into error.
For instance, if the number of variables on the left-hand side is less than the number of elements in the iterable object, the program will run into a ValueError exception saying that there are too many values to unpack. You can observe this in the following example.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
a,b,c,d,e=myList
print("The variables are:")
print(a,b,c,d,e)
Output:
The list is:
[1, 2, 3, 4, 5, 6]
ValueError Traceback (most recent call last)
/tmp/ipykernel_16212/706151425.py in <module>
2 print("The list is:")
3 print(myList)
----> 4 a,b,c,d,e=myList
5 print("The variables are:")
6 print(a,b,c,d,e)
ValueError: too many values to unpack (expected 5)
In the about code, you an observe that there are six elements in the list but we have only five variables. Due to this, the program runs into a Python ValueError exception saying that there are too many values to unpack.
In a similar manner, if the number of variables on the left side of the assignment operator is greater than the number of elements in the iterable object, the program will run into a ValueError exception saying that there are not too many values to unpack. You can observe this in the following example.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
a,b,c,d,e,f,g=myList
print("The variables are:")
print(a,b,c,d,e,f,g)
Output:
The list is:
[1, 2, 3, 4, 5, 6]
ValueError Traceback (most recent call last)
/tmp/ipykernel_16212/1196283615.py in <module>
2 print("The list is:")
3 print(myList)
----> 4 a,b,c,d,e,f,g=myList
5 print("The variables are:")
6 print(a,b,c,d,e,f,g)
ValueError: not enough values to unpack (expected 7, got 6)
In the above example, there seven variables on the left hand side and only six elements in the list. Due to this, the program runs into ValueError exception saying that there aren’t enough values to unpack.
Unpacking Using The * Operator in Python
When we have less number of variables than the elements in the iterable object, we can use the * operator to unpack the iterable object using the following syntax.
var1, var2, var3,var4..varN, *var=iterable_object
In the above syntax, If there are more than N elements in the iterable_object
, first N objects are assigned to the variables var1
to varN
. The rest of the variables are packed in a list and assigned to the variable var
. You can observe this in the following example.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
a,b,c,*d=myList
print("The variables are:")
print(a,b,c,d)
Output:
The list is:
[1, 2, 3, 4, 5, 6]
The variables are:
1 2 3 [4, 5, 6]
In the above example, we have six elements in the list. On the left hand side, we have four variables with the last variable containing the * sign. You can observe that the three variables are assigned individual elements whereas the variable containing the * operator gets all the remaining elements in a list.
Now, let us move the variable containing the * operator to the start of the expression as shown below.
*var, Var1, var2, var3,var4..varN =iterable_object
In this case, if there are more than N elements in the iterable_object
, the last N elements are assigned to the variables var1
to varN
. The remaining elements from the start are assigned to variable var
in a list. You can observe this in the following example.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
*a,b,c,d=myList
print("The variables are:")
print(a,b,c,d)
Output:
The list is:
[1, 2, 3, 4, 5, 6]
The variables are:
[1, 2, 3] 4 5 6
We can also put the variable containing the * operator in between the variables on the left-hand side of the assignment operator. For example, consider the following syntax.
var1, var2, var3…varM, *var, varM+1,varM+2…varN=iterable_object
In the above example, there are M variables on the left-hand side of var and N-M variables on the right-hand side of var
. Now, if the object iterable_object
has more than N elements,
- First M elements of
iterable_object
are assigned to the variablesvar1
tovarM
. - The last N-M variables in
iterable_object
are assigned to the variablesvarM+1
tovarN
. - The rest of the elements in the middle are assigned to the variable
var
as a list.
You can observe this in the following example.
myList=[1,2,3,4,5,6]
print("The list is:")
print(myList)
a,b,*c,d=myList
print("The variables are:")
print(a,b,c,d)
Output:
The list is:
[1, 2, 3, 4, 5, 6]
The variables are:
1 2 [3, 4, 5] 6
Conclusion
In this article, we discussed how to use the unpacking operator in Python. The unpacking operation works the same in lists, sets, and tuples. In dictionaries, only the keys of the dictionary are unpacked when using the unpacking operator.
To learn more about Python programming, you can read this article on tuple comprehension in Python. You might also like this article on Python continue vs break statements.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.