N Kaushik

Fix - ValueError Too many values to unpack, expected 2

June 28, 2022

ValueError: Too many values to unpack, expected 2:

ValueError: Too many values to unpack (expected 2), you will get this error if you are trying to unpack too many values in Python. Your code is expecting only 2 values, but you are trying to unpack two values from more than two values.

For example, you are calling a function that should return 2 values and you are trying to unpack these two values. But, the function returned 3 values. So, the unpacking will fail and it will throw this error:

ValueError: too many values to unpack (expected 2)

What is unpacking and how it is used:

In python, unpacking is the process of assigning an iterable of values to a list or tuple. With unpacking, we can return multiple values from a function and unpack these to different variables.

Let me show you how we can use unpacking with different type of values:

Example 1: Unpacking a tuple:

The following example unpacks a tuple:

a, b = (1, 2)
(c, d) = (3, 4)
[e, f] = (5, 6)

print(f'a: {a}, b: {b}, c: {c}, d: {d}, e: {e}, f: {f}')

The first and the second lines are equivalent. We are unpacking a tuple and the values are assigned to another tuple.

The third line is also unpacking the right tuple. But it is assigning the values to the variables of a list.

It will print:

a: 1, b: 2, c: 3, d: 4, e: 5, f: 6

Example 2: Unpacking a list:

Similar to tuples, we can unpack a list:

a, b = [1, 2]
(c, d) = [3, 4]
[e, f] = [5, 6]

print(f'a: {a}, b: {b}, c: {c}, d: {d}, e: {e}, f: {f}')

It will print the same output.

Example 3: Unpacking a dictionary:

Unpacking also works with a dictionary. We can unpack the keys or values of a dictionary as like below:

dic = {1: 'one', 2: 'two'}

a, b = dic
(c, d) = dic.values()
[e, f] = dic.items()

print(f'a: {a}, b: {b}, c: {c}, d: {d}, e: {e}, f: {f}')

The first unpacks the keys, the second one unpacks the values, and the third one unpacks the key-value pairs.

a: 1, b: 2, c: one, d: two, e: (1, 'one'), f: (2, 'two')

Unpacking using a dummy variable or underscore:

While unpacking, we can use an underscore, _, or a dummy variable. It will hold the unpacked data and in the future, if we want to use any other variable, we can replace the underscore with that variable.

a, b, _ = (1, 2, 3)

print(f'a: {a}, b: {b}, _: {_}')

It holds the value but if you want to use it, you can replace it with any other variable name.

Unpacking using star or *:

One variable can hold multiple values while unpacking. We have to use *** before the variable name. For example,

a, *b = (1, 2, 3, 4, 5, 6, 7, 8)
*c, d = (1, 2, 3, 4, 5, 6, 7, 8)
*e, f, g = (1, 2, 3, 4, 5, 6, 7, 8)

print(f'a: {a}, b: {b}')
print(f'c: {c}, d: {d}')
print(f'e: {e}, f: {f}, g: {g}')

It will print:

a: 1, b: [2, 3, 4, 5, 6, 7, 8]
c: [1, 2, 3, 4, 5, 6, 7], d: 8
e: [1, 2, 3, 4, 5, 6], f: 7, g: 8

Reason of ValueError: Too many values to unpack, expected 2:

The error ValueError: Too many values to unpack, expected 2 occurs if we try to unpack two values but the returned iterator contains more than two values.

ValueError on unpacking list elements:

Let’s say we are trying to unpack 2 elements and the list holds more than 2.

a, b = [1, 2, 3]
a, b = [1, 2, 3, 4, 5]
a, b = [1]

The first two examples will throw the same error:

ValueError: too many values to unpack (expected 2)

But, the last one will throw a different error:

ValueError: not enough values to unpack (expected 2, got 1)

Too many values to unpack expected 2

The reason is the same. The unpacking should be done for all elements.

To solve this issue,

  • We can use different variables to hold all values.
  • We can either use dummy variables or underscores.
  • We can use an asterisk variable to hold a list of values.
a, b, temp = [1, 2, 3]
a, b, _, _, _ = [1, 2, 3, 4, 5]
a, b, *_ = [1, 2, 3, 4, 5]

Each of these examples will work.

ValueError on unpacking a string:

This is similar to the above example. Let’s consider the below example:

a, b, c = '1234'

It will throw:

ValueError: too many values to unpack (expected 3)

A similar change as like the above example will fix this.


Subscribe to my Newsletter