Iterable unpacking

The unpacking technique is used to dismember a collection. A simple example is the following:

collection = (3, 4, 5)
print(f"Collection: {collection}")
a, b, c = collection
print(f"Unpacked: a = {a}, b = {b}, c = {c}")
Collection: (3, 4, 5)
Unpacked: a = 3, b = 4, c = 5

Comprehension is a concise technique to create iterable objects, by applying an expression to each item in an existing iterable (and optionally filtering). For example:

x = [1, 2, 3, 4]
y = [i**2 for i in x]
print(f"y = {y}")
y = [1, 4, 9, 16]

Finally, iterable unpacking is a sort of combination between unpacking and comprehension. It allows us to create an iterable object by unpacking an iterable generator. Below, we define a generator of Fibonacci numbers, and use is to build a list through iterable unpacking:

def fibonacci_generator(index):
    current_fibonacci, next_fibonacci = 0, 1
    for _ in range(0, index):
        fibonacci_number = current_fibonacci
        current_fibonacci, next_fibonacci = next_fibonacci, current_fibonacci + next_fibonacci
        yield fibonacci_number

print([*fibonacci_generator(10)])        
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

In the code above, the asterisk (*) is known as the iterable unpacking operator

Author: Oscar Castillo-Felisola

Created: 2026-04-02 Thu 14:59