In Python, the unpacking assignment is a powerful feature that allows you to assign multiple values to multiple variables in a single
statement.
The basic rule for the unpacking assignment is that the number of variables on the left-hand side must be equal to the number of elements in the
iterable. If this is not respected, a ValueError
will be produced at runtime.
Code examples
Noncompliant code example
def foo(param):
ls = [1, 2, 3]
x, y = ls # Noncompliant: 'ls' contains more elements than there are variables on the left-hand side
Compliant solution
def foo(param):
ls = [1, 2, 3]
x, y, z = ls