Python provides us with different shorthand methods to perform various tasks. In this article, we will discuss tuple unpacking in Python with examples.
What is Unpacking in Python?
In Python, the unpacking operation is used to assign elements of a collection object like a list, tuple, dictionary, etc directly to different variables. We use the Python unpacking operator and parallel assignment to unpack collection objects.
Let us discuss both ways for tuple unpacking in Python.
Tuple Unpacking Using Parallel Assignment
If a tuple has N elements, we can perform tuple unpacking in Python using parallel assignment as shown in the following syntax.
var1, var2, var3,...., varN-1, varN=(e1, e2, e3,.....,eN-1, eN)
In the above syntax, the number of variables on the left-hand side of the assignment operator must be equal to the number of elements in the tuple on the right side of the assignment operator.
After tuple unpacking, the elements in the tuple are assigned to the variables at the corresponding position on the left-hand side of the assignment operator. The first element of the tuple is assigned to the first variable, the second element of the tuple is assigned to the second variable on LHS, and so on. You can observe this in the following example.
myTuple=(1,12,11,14)
print("The tuple is:",myTuple)
a,b,c,d=myTuple
print("The variables are:",a,b,c,d)
Output:
The tuple is: (1, 12, 11, 14)
The variables are: 1 12 11 14
In the above example, we have defined a tuple with four elements. Then, we unpack the tuple using the parallel assignment. You can observe that the elements in the tuple are assigned to the variable at the same position on the left-hand side of the assignment operator.
In the parallel assignment operation, if the number of variables is less than the elements in the tuple, the program runs into a Python ValueError exception. You can observe this in the following example.
myTuple=(1,12,11,14,15,-1)
print("The tuple is:",myTuple)
a,b,c,d=myTuple
print("The variables are:",a,b,c,d)
Output:
The tuple is: (1, 12, 11, 14, 15, -1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_115443/3055827636.py in <module>
1 myTuple=(1,12,11,14,15,-1)
2 print("The tuple is:",myTuple)
----> 3 a,b,c,d=myTuple
4 print("The variables are:",a,b,c,d)
ValueError: too many values to unpack (expected 4)
In the above code, you can observe that the tuple contains six elements whereas there are only four variables on the left side of the assignment operator during tuple unpacking. Hence, the program runs into a ValueError exception.
In a similar manner, if the number of variables exceeds the number of elements in the tuple, the program runs into a ValueError exception saying that there aren’t enough values to unpack. You can observe this in the following example.
myTuple=(1,12,11,14)
print("The tuple is:",myTuple)
a,b,c,d,e,f=myTuple
print("The variables are:",a,b,c,d)
Output:
The tuple is: (1, 12, 11, 14)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_115443/1429134448.py in <module>
1 myTuple=(1,12,11,14)
2 print("The tuple is:",myTuple)
----> 3 a,b,c,d,e,f=myTuple
4 print("The variables are:",a,b,c,d)
ValueError: not enough values to unpack (expected 6, got 4)
Here, we have only four elements in the tuple. However, the assignment statement contains six variables. Due to this, the program runs into a ValueError exception.
Tuple Unpacking Using The * Operator
If we have fewer variables than the number of elements in the tuple, we can use the * operator for tuple unpacking in Python. For tuple unpacking using the * operator, we will use the following syntax
var1, var2, var3,var4..varN, *var=myTuple
Here, if the tuple myTuple contains more than N variables, the first N elements of the tuple are assigned to variables var1, var2,.. till varN. The rest of the elements are stored in the variable var as a list. You can observe this in the following example.
myTuple=(1,12,11,14,123,32,76,114,56)
print("The tuple is:",myTuple)
a,b,c,*d=myTuple
print("The variables are:",a,b,c,d)
Output:
The tuple is: (1, 12, 11, 14, 123, 32, 76, 114, 56)
The variables are: 1 12 11 [14, 123, 32, 76, 114, 56]
In the above example, there are nine elements in the tuple. On the contrary, we have only four variables. You can observe that the fourth variable d in the code uses the * operator. Hence, first three elements of the tuple are assigned to the variables a, b, and c. The rest of the elements are packed in a list and assigned to the variable d.
Instead of the above approach, you can also use the following syntax for tuple unpacking in Python.
*var, var1, var2, var3,var4..varN =myTuple
When we use the above syntax, the last N elements of the tuple myTuple are assigned to the variables var1, var2, till varN. The remaining elements of the tuple from starting index are stored in the variable var as a list. You can observe this in the following example.
myTuple=(1,12,11,14,123,32,76,114,56)
print("The tuple is:",myTuple)
*a,b,c,d=myTuple
print("The variables are:",a,b,c,d)
Output:
The tuple is: (1, 12, 11, 14, 123, 32, 76, 114, 56)
The variables are: [1, 12, 11, 14, 123, 32] 76 114 56
In the above example, the first variable in the assignment statement uses the * operator. Hence, the last three elements of the tuple are assigned to variables b, c, and d. Rest of the elements are packed into a list and assigned to the variable a.
You can also choose to put the variable containing the * operator in the middle of the variables on the left-hand side of the assignment operator. For instance, consider the following syntax.
var1,var2,var3…varM,*var,varM+1,varM+2…varN=myTuple
If we use the above syntax, the first M elements of myTuple are assigned to the variables var1, var2, till varM. The last N-M elements are assigned to the variables varM+1, varM+2,… till varN. The rest of the elements in the middle of the tuple are assigned to the variable var in a list. You can observe this in the following example.
myTuple=(1,12,11,14,123,32,76,114,56)
print("The tuple is:",myTuple)
a,b,*c,d=myTuple
print("The variables are:",a,b,c,d)
Output:
The tuple is: (1, 12, 11, 14, 123, 32, 76, 114, 56)
The variables are: 1 12 [11, 14, 123, 32, 76, 114] 56
In this example, we have used the * operator with the third variable in the assignment statement. Hence, the first two elements of the tuple are assigned to variables a, and b. The last element of the tuple is assigned to the variable d. Rest of the elements are assigned in a list to the variable c.
Unpack Tuple into a List in Python
You can also use the tuple unpacking operation to create a list directly from a tuple. For this, we will use the following syntax.
myList=[*myTuple]
After executing the above statement, we get a list myList containing elements from the tuple myTuple.
myTuple=(1,12,11,14,123,32,76,114,56)
print("The tuple is:",myTuple)
myList=[*myTuple]
print("The list is:",myList)
Output:
The tuple is: (1, 12, 11, 14, 123, 32, 76, 114, 56)
The list is: [1, 12, 11, 14, 123, 32, 76, 114, 56]
Unpack Tuple into a Set
Similar to a list, we can also create a set from a tuple using the unpacking operator in Python as shown below.
myTuple=(1,12,11,14,12,32,76,11,56)
print("The tuple is:",myTuple)
mySet={*myTuple}
print("The set is:",mySet)
Output:
The tuple is: (1, 12, 11, 14, 12, 32, 76, 11, 56)
The set is: {32, 1, 11, 12, 76, 14, 56}
Conclusion
In this article, we discussed the basics of tuple unpacking in Python. To learn more about tuples, you can read this article on tuple comprehension. You might also like this article on tuple vs list in Python.
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.