Python Tuple Unpacking: 2 Quick Methods for Unpacking Tuples

Python (1)

This Article is going to clarify different ways to unpack a Tuple, following certain examples on our code snippet. Let’s get into it without wasting time.

Before getting started, First of all, Let’s understand what a Tuple is in python.

A tuple is one of the four popular in-built collections in Python among List, Set, and Dictionary. It is used to store ordered, indexed collection of data irrespective of datatypes.

Similar to Arrays, the first value in the Tuple will have the index [0], the second value [1], and so on. A Tuple can be created by following commands.

new_tuple = ("New Delhi", "Tiger", 5000, "5.285", "Rahul", True)

Our new_tuple will be created as follows.

print(new_tuple)
 ('New Delhi', 'Tiger', 5000, '5.285', 'Rahul', True)
Sample Tuple
Sample Tuple

Some key features of a Tuple in Python are as follows.

  • It’s immutable in nature. Once a Tuple is created, it is neither possible to modify its elements nor to delete.
  • Deleting an element from a Tuple is not possible. But deleting a Tuple is.
  • Tuples can store duplicate values.
  • We can create an empty Tuple.
  • We can create a Tuple with a single element.
  • Concatenation is possible in between two or more Tuples.
  • Tuples do not support resizing methods: append(), remove()pop() etc.

As you know A Tuple is immutable in nature. Its values or contents can not be changed once created. But one thing we can do is, We can unpack the contents of a Tuple. Unpacking a Tuple means splitting the Tuple’s contents into individual variables. We are going to learn here three methods to do the same followed by some examples. Let’s understand them as well.

Unpacking a Python Tuple By Reassigning

#Let's create a tuple
new_tuple = ("New Delhi", "Tiger", 5000, "5.285", "Rahul", True)

#We can unpack the tuple by assigning each content of the tuple to individual variables 
(a, b, c, d, e, f) = new_tuple

In the above example, We assigned each content of the Tuple to individual variables. Let’s see the contents of each variable by following the code snippet.

#printing each variable 
print(a)
'New Delhi'
print(b)
'Tiger'
print(c)
5000
print(d)
'5.285'
print(e)
Rahul
print(f)
True

While using this method, We must remember that We need to take the no. of variables the same as the no. of contents inside the Tuple. otherwise, it will throw errors as follows.

(a, b, c, d) = new_tuple
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    (a, b, c, d) = new_tuple
ValueError: too many values to unpack (expected 4)
Error( Two Many Values to Unpack )
Error( Two Many Values to Unpack )

Unpacking a Tuple Using the Asterisk * sign

In this example, We are going to use the Asterisk (*) operator. If the number of variables is less than the number of contents inside our Tuple, We can add a * to the variable name and the values will be assigned to the variable as a list. Let’s try this as the following code snippet.

#taking the same tuple created in method 1
print(new_tuple)
 ('New Delhi', 'Tiger', 5000, '5.285', 'Rahul', True)

#no. of variables (=3) is less than no. of contents (=6).
#adding * to y
(x, *y, z) = new_tuple

Now, We can see the result by printing the variables individually.

print(x)
New Delhi
print(y)
['Tiger', 5000, '5.285', 'Rahul']
print(z)
True

We can see that the four contents in between created a list and assigned to y. In this way, This method works. We can understand better by following the below examples in our code snippet.

#Example 1 
(x, y, *z) = new_tuple
print(x)
'New Delhi'
print(y)
'Tiger'
print(z)
[5000, '5.285', 'Rahul', True]

#Example 2 
(*x, y, z) = new_tuple
print(x)
['New Delhi', 'Tiger', 5000, '5.285']
print(y)
'Rahul'
print(z)
True

#Example 3
(x, y, *z, w) = new_tuple
print(x)
New Delhi
print(z)
[5000, '5.285', 'Rahul']
print(w)
True
print(y)
Tiger

Note: While using this method, add the * operator to a single variable only. Else, it will throw an error as follow.

(*x, *y, z) = new_tuple
SyntaxError: multiple starred expressions in assignment
Multisigned Error
Multisigned Error

Conclusion

In this article, We covered how to unpack a Tuple in Python. Tuples are of advantage when used for the use case it is designed for like other elements in Python. Hope You must have enjoyed it. We must visit again with some more exciting topics.