Iterable Unpacking in Python #PyTip02

vivekthedev

Vivek

Posted on August 5, 2022

Iterable Unpacking in Python #PyTip02

Python provides an awesome variety of sequential data structures. The most common of them are list and tuple. These sequences are sometimes used to store records, which later needs to be unpacked.

When unpacking the values from a sequence you may encounter error like-ValueError: too many values to unpack. In this short post I will share a short PyTip to avoid this error.

Problem

Create a file or run the below code in the Python REPL.

primes = [2, 3, 5, 7, 11]
first, second, third = primes
Enter fullscreen mode Exit fullscreen mode

The above code will produce the following error.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)
Enter fullscreen mode Exit fullscreen mode

The above error states that to unpack your iterable, your iterable size must be three.

Solution

To solve this problem PEP 3132 was proposed. Now, write the following code in Python REPL to see Extended Iterable Unpacking in action.

>>> primes = [2, 3, 5, 7, 11]
>>> first, second, third, *_ = primes
>>> print(first, second, third, _)
2 3 5 [7, 11]
Enter fullscreen mode Exit fullscreen mode

In the above output you can see that *_ syntax caught all the excess values from the iterable. The _ variable could be anything of your choice, it doesn't have to be an underscore. Additionally, "*_" may be used anywhere in the statement. Consider the example below to get the idea.

>>> primes = [2, 3, 5, 7, 11]
>>> first, *_ , last = primes
>>> print(first, last, _)
2 11 [3, 5, 7]
Enter fullscreen mode Exit fullscreen mode

I find this feature extreamly useful and hope you find it useful too. Thanks for the reading today's PyTip.

💖 💪 🙅 🚩
vivekthedev
Vivek

Posted on August 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related