Python - Unpacking

Khalimbetov Ulgubek
3 min readJan 12, 2021

In this mini tutorial we will go step by step to fully understand haw unpacking works in Python.

First of all, if we assign (=) multiple values separated by comma to a variable. Python automatically converts them to a tuple.

records = 1, 2, 3
tuple_records = (1, 2, 3)
print(records == tuple_records)
print(f"type:{type(records)}, {records}")
#Output:
True
type:<class 'tuple'>, (1, 2, 3)

variable record and tuple_records are both tuple, if we dont specify record explicitly.

Tuple unpacking

Any sequence(or iterable) can be unpacked into variables using an assignment operation. If you quite unsure what iterable object is, In the Python world, an iterable is any object that you can loop over with a for loop. Iterables are not always indexable, they don’t always have lengths, and they’re not always finite. The only one requirement is that the length of iterables have to correspond the number of variables we assign to.

tuple_ = (8, 3)
eight, three = tuple_
print(eight, three)
#Output: 8 3

In the example below we will use a string.

f1, s2, t3, f4, f5 = 'Hello'
print(f'f1 = "{f1}", f5 = "{f5}"')
#Output: f1 = "H", f5 = "o"

Unpacking Elements From Iterables of Arbitrary Length.

Suppose, you want to get as an output from a list, only that elements , which starts from first index until a last element, but you do not the length of a list. Here unpacking comes in:

list_of_numbers = (1, 2, 3, 4, 5, 6)
first, *middle, last = list_of_numbers
print(middle, type(middle))
#Output: [2, 3, 4, 5] <class 'list'>

You can ask, why do we need to assign to a variable? Of course, we can assign it manually by slicing a tuple. For example:

But the reason why we use ‘star expression’, because we can access all elements from iterable easily, and do not type manually.

Python “star expression” is useful to unpack elements from iterables of arbitrary length.

record = ('Elon', 'Elon@gmail.com', '777-321-3212', '3232-122-5232')
name, email, *number = record
print(number)
#Output: ['777-321-3212', '3232-122-5232']

It is worth mention that Output of variable number will always be a list, even it is nothing to unpack.

person_information = ('Korea', 'Seoul')
country, city, *number = person_information
print(f'country: {country}, city: {country}, number: {number}')
#Output: country: Korea, city: Seoul, number: []

To better understand these concepts, we will start by doing problems.

Problem: Find average of a list

def findSum(*collection_of_numbers):
return sum(collection_of_numbers) // len(collection_of_numbers)
print(findSum(1, 2, 3, 4, 5, 6))
#Output: 3

We can go even further and manipulate inner iterable object.

gpa_of_students = [
('Jin', 'Alian', 3.5),
('Kirigaya', 'Minato', 4.0),
('Sam', 'Uil', 4.5),
]

for *name, gpa in gpa_of_students:
if gpa >= 4:
print(f'Full Name: {" ".join(name)}, GPA: {gpa}')
#Output:Full Name: Kirigaya Minato, GPA: 4.0
#Output:Full Name: Sam Uil, GPA: 4.5

I use “ ”.join(name), because *name is a list. if we does not use join, it will print: [‘Kirigaya’, ‘Minato’]

--

--

Khalimbetov Ulgubek
0 Followers

Web Developer, Student at Sejong University