ValueError: too many values to unpack (expected 3) in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

ValueError: too many values to unpack (expected 3) in Python #

The Python "ValueError: too many values to unpack (expected 3) in Python" occurs when the number of variables in the assignment is not the same as the number of values in the iterable. To solve the error, declare exactly as many variables as there are items in the iterable.

valueerror-too-many-values-to-unpack-expected-3

Here is an example of how the error occurs.

main.py
# ⛔️ ValueError: too many values to unpack (expected 3) r, g, b = ['red', 'green', 'blue', 'yellow']
We declare 3 variables, but the list contains 4 items. The inconsistency between the number of variables and items in the list causes the error.

To solve the error, make sure to declare exactly as many variables as there are items in the iterable.

main.py
r, g, b, y = ['red', 'green', 'blue', 'yellow'] print(r) # 👉️ 'red' print(g) # 👉️ 'green' print(b) # 👉️ 'blue' print(y) # 👉️ 'yellow'

The error is also caused when we try to unpack values from a string.

main.py
# ⛔️ ValueError: too many values to unpack (expected 3) r, g, b = 'red,green,blue'
When unpacking from a string, each variable declaration counts for a single character.

To solve the error in the example, we have to split the string on the comma before unpacking the values.

main.py
my_list = 'red,green,blue'.split(',') print(my_list) # 👉️ ['red', 'green', 'blue'] r, g, b = my_list print(r) # 👉️ 'red' print(g) # 👉️ 'green' print(b) # 👉️ 'blue'

The str.split() method splits the string into a list of substrings using a delimiter.

If you meant to unpack specific characters into variables, make sure to declare exactly as many variables as there are characters in the string.

main.py
my_str = 'abc' a, b, c = my_str print(a) # 👉️ 'a' print(b) # 👉️ 'b' print(c) # 👉️ 'c'

The error also occurs when we try to unpack the items from a function's return value.

main.py
def get_tuple(): return ('red', 'green', 'blue', 'yellow') # ⛔️ ValueError: too many values to unpack (expected 3) r, g, b = get_tuple()

Make sure to declare exactly as many variables as there are items in the iterable that the function returns.

main.py
def get_tuple(): return ('red', 'green', 'blue', 'yellow') r, g, b, y = get_tuple() print(r) # 👉️ 'red' print(g) # 👉️ 'green' print(b) # 👉️ 'blue' print(y) # 👉️ 'yellow'

The function returns a tuple containing 4 elements, so we have to declare exactly 4 variables.

Conclusion #

The Python "ValueError: too many values to unpack (expected 3) in Python" occurs when the number of variables in the assignment is not the same as the number of values in the iterable. To solve the error, declare exactly as many variables as there are items in the iterable.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.