Bug of the week #6


Welcome to the next pikoTutorial!

The error we’re handling today is a Python runtime error:

ValueError: too many values to unpack

What does it mean?

The “too many values to unpack” error typically occurs when there is a mismatch between the number of variables or elements being unpacked and the number of values being assigned. This situation commonly arises in unpacking sequences like tuples or lists during assignment operations.

How to fix it?

Let’s look at the following code:

a, b = [1, 2, 3]

In this example a list from which we’re taking values has 3 elements, but we’ve provided only to variables to store them (a and b), so the value 3 has no place to go. To fix this, either add an additonal variable or reduce the number of elements in the list.

a, b, c = [1, 2, 3]
a, b = [1, 2]