- Published on
Python Tuple Unpacking: A Detailed Exploration
In python tuples are a collection of objects which are ordered and immutable. Tuples are sequences, just like lists. Tuples are much similar to lists. The only differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
Python tuple unpacking means pulling values apart from a tuple or tuple unpacking means pulling values apart from the tuple and assigning it to separate variables. unpacking is not only used for tuples but also for lists and string etc In this article, you will learn about Python tuples, tuple unpacking.
For example, here is a tuple of the numbers from 1 up to 5
nums = (1, 2, 3, 4, 5)
We can also create a tuple without parentheses.
For example, you can create modify the above tuple with:
nums = 1, 2, 3, 4, 5
Tuple Unpacking
If you need to pull out values of tuple into variables, use tuple unpacking. To do this, declare a comma-separated group of variable names and assign it to the tuple.
When unpacking the number of variables must match the number of items in the tuple.
For example, let’s consider 3D coordinate and let’s unpack the values from the tuple into separate variables x, y, and z:
coords = 1, 2, 3
x, y, z = coords
This assigns the value 1 to x, 2 to y, and 3 to z.
But While unpacking, always make sure the number of variables must match the number of values in the tuple, if not, you must use an asterisk to collect the remaining values as a list.
For example, let’s say you have three variables and a tuple with 6 values so when unpacking the tuple we have 3 additional values so we can use an asterisk to collect the remaining values as a list.
Let’s unpack the three first coordinate values into separate variables x, y, and z, and then store the rest of the values into a list:
coords = 1, 2, 3, 4, 5, 6
x, y, z, *rest = coords
Here 1 is assigned to x, 2 to y, 3 to z, and the rest of the values into rest.
You can verify this by printing the values:
coords = 1, 2, 3, 4, 5, 6
x, y, z, *rest = coords
print(f"x: {x}")
print(f"y: {y}")
print(f"z: {z}")
print(f"rest: {rest}")
Output
x: 1
y: 2
z: 3
rest: [4, 5, 6]
Let’s see what happens when the number of variables exceeds the number of elements in the tuple:
coords = 1, 2, 3
x, y, z, i, j = coords
Output:
ValueError: not enough values to unpack (expected 6, got 3)
As you can see, the unpacking failed because there are not enough values in the tuple to be assigned to 5 variables.
The Above example is the number of variables is greater than the tuple size but Also, if the number of new variables is less than the number of elements in the tuple, unpacking also fails.
For example
coords = 1, 2, 3, 4, 5
x, y = coords
Output:
ValueError: too many values to unpack (expected 2)
Here the error says there are too many values to unpack. In the tuple, there are 5 values in total, whereas we only declare two variables to store them all.
Unpacking Other Iterables in Python
Even though will call this article python tuple unpacking unpacking also works for other iterable types as well like lists and sets
Let’s see some basic examples of unpacking with other common iterable types in Python:
Example Unpacking List
nums = [1, 2, 3]
x, y, z = nums
print(f"x:{x}, y:{y}, z:{z}")
Output:
x:1, y:2, z:3
Example Unpacking String
string = "Hey"
l1, l2, l3 = string
print(f"letter 1:'{l1}', letter 2:'{l2}', letter 3 :'{l3}'")
Output
letter 1:'H', letter 2:'e', letter 3 :'y'
Example Unpacking Dictionary
data = {"name": "Alice", "age": 30}
(k1, v1), (k2, v2) = data.items()
print(f"key1: {k1}, value1: {v1}")
print(f"key1: {k2}, value1: {v2}")
key1: name, value1: Alice
key1: age, value1: 30
Conclusion
In this article you have learned how to create tuples in python with and without parentheses. You also learned Unpacking tuples means assigning individual elements of a tuple to multiple variables and also using the * operator to assign remaining elements of an unpacking assignment into a list and assign it to a variable.