ValueError

ValueError, unlike TypeError, occurs not because the data type is incorrect, but because the value is incorrect. For example, when we attempt to divide a list by a number we receive a TypeError because a list is the wrong type for division. A list, however, is the right type to support multiple variable assignment but needs the correct number of values to unpack:

>>> x, y, z = [1, 2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>> x, y = [1, 2, 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> x, y, z = [1, 2, 3]
>>> print x, y, z
1 2 3

Get Learning Python for Forensics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.