Beginner Level
Intermediate Level
Advanced Level
Introduction
One of the most useful features of Python is its ability to work with tuples - an ordered sequence of elements, typically used to group related data together. In this tutorial, we will explore a particularly handy technique known as unpacking tuples. Unpacking tuples is the process of extracting individual elements from a tuple and assigning them to separate variables. This can be incredibly useful when working with large datasets, as it allows us to manipulate and analyze data in a more efficient manner.
Table of Contents :
- Unpacking Tuples
- Alternate syntax of unpacking tuples
- Unpacking - packing
- Merging tuples
Unpacking Tuples :
- Note # : We are discussing the topic of unpacking tuples in the section of functions because this concept forms the base of the concept of variadic functions that we'll study in the next tutorial.
- The unpacking of tuples is a strong mechanism in Python that can be used in various scenarios.
- The basic syntax of unpacking tuples is :
var_1, var_2, var_3 = tuple_name
- The number of variables should be equal to the number of elements in the tuple.
- If the number of variables is less than the number of elements of the tuple, python interpreter will raise an error :
ValueError: too many values to unpack
- If the number of variables is more than the number of elements of the tuple, python interpreter will raise an error :
ValueError: not enough values to unpack
- Code Sample :
tup = (10, 20, 30)
var_1, var_2, var_3 = tup
print(var_1, var_2, var_3)
# Output
# 10 20 30
Alternate syntax of unpacking tuples :
- The construct :
var = 10, 20, 30
defines a tuple. - If we check the data type of variable
var
it comes out to be tuple.
var = 10, 20, 30
print(type(var))
# Output
# <class 'tuple'>
- Hence another syntax of tuple unpacking is :
var_1, var_2 = value_1, value_2
- Here :
- The left hand side var_1, var_2 is a tuple
- The right hand side value_1, value_2 is also a tuple
- Code Sample :
var_1, var_2 = 10, 20
print(var_1, var_2)
# Output
# 10 20
Unpacking - packing
- There is another syntax in python in which we can
- assign the first few elements of the tuple to variables
- and save the rest as a slice of the list.
- We use an asterisk for this purpose.
- The syntax for tuple unpacking-packing is :
var_1, var_2, *var_3 = tuple_name
- Here :
- the first element of the tuple is assigned to var_1
- the second element of the tuple is assigned to var_2
- the rest of the elements are packed as a new list and assigned to var_3
- In this process, the python interpreter :
- first unpacks the original tuple
- assigns the matching number of elements to variables.
- packs the remaining elements to a new list and assign it to the variable with asterisk.
- We can have only one variable with asterisk in this syntax.
- If we try to put asterisk on more than one variable, python interpreter will raise an error -
SyntaxError: multiple starred expressions in assignment
- We can put asterisk on any of the variable.
- Code Sample :
tup = (10, 20, 30, 40, 50, 60)
var_1, var_2, *var_3 = tup
print(var_1)
print(var_2)
print(var_3)
# Output
# 10
# 20
# [30, 40, 50, 60]
Merging Tuples
- Asterisk can be used to merge two tuples as well
- The syntax of merging two tuples is :
new_tuple = (*tuple_1, *tuple_2)
- Code Sample :
tup_1 = (10, 20, 30)
tup_2 = (40, 50, 60)
new_tuple = (*tup_1, *tup_2)
print(new_tuple)
# Output
# (10, 20, 30, 40, 50, 60)
Prev. Tutorial : Lambda expressions
Next Tutorial : Variadic Functions