How to Solve ValueError: Too Many Values to Unpack (Expected 2) in Python Dictionaries

Olorunfemi Akinlua Feb 02, 2024
  1. Solve ValueError: too many values to unpack (expected 2) Using Dictionary Keys
  2. Solve ValueError: too many values to unpack (expected 2) Using Dictionary Values
  3. Solve ValueError: too many values to unpack (expected 2) Using Dictionary items()
How to Solve ValueError: Too Many Values to Unpack (Expected 2) in Python Dictionaries

When we deal with dictionaries or lists in Python, there is a high likelihood that ValueError will happen. Often, this is due to passing the wrong index or property when accessing a value.

With dictionaries, when looping through the values, the appropriate methods to traverse dictionaries need to be understood to prevent ValueError or the too many values to unpack (expected 2) error messages.

This article will discuss three methods to traverse or access dictionary properties and values and prevent the ValueError: too many values to unpack (expected 2) error.

Solve ValueError: too many values to unpack (expected 2) Using Dictionary Keys

Before we solve the error message, let us replicate the scenario that causes the ValueError: too many values to unpack (expected 2) error.

If we have a dictionary and want to loop through the keys and values and plan to print out the keys and values. Some newbies might be tempted to try the code snippet below:

dictionary = {"color": "blue", "shape": "square", "volume": 40}

for key, values in dictionary:
    print("Key: " + key)
    print("Value: " + str(values))
    print("===")

The output of the code:

Traceback (most recent call last):
  File "c:\Users\akinl\Documents\HTML\python\dict.py", line 8, in <module>
    for key, values in dictionary:
ValueError: too many values to unpack (expected 2)

The error message, ValueError: too many values to unpack (expected 2) is shown within your output area, and to solve this, we need to understand that the for/in loop works by iterating over iterable (list, tuple, set, etc.).

However, in the case of a dictionary, only the keys are iterable directly, without any method. Therefore, to make the code work, we need to access only one variable to access the key, key, and not two to access the key and value.

Now, what we are left with is how to access the value. To access the value, we can use the single variable, key, within the square bracket notation, dictionary[key].

dictionary = {"color": "blue", "shape": "square", "volume": 40}

for key in dictionary:
    value = dictionary[key]
    print("Key: " + key)
    print("Value: " + str(value))
    print("===")

The output of the code:

Key: color
Value: blue
===
Key: shape
Value: square
===
Key: volume
Value: 40
===

With the simple code change, we eliminated the ValueError: too many values to unpack (expected 2) error and iterated over the dictionary object within Python without any issues.

Solve ValueError: too many values to unpack (expected 2) Using Dictionary Values

Using the same scenario, we can access the values directly using the values() method that comes with the dictionary object. With this, we have access to the values instead of the keys.

Therefore, for the same scenario, we can only access the values. It is useful in cases where only the values are useful.

dictionary = {"color": "blue", "shape": "square", "volume": 40}

for value in dictionary.values():
    print("Value: " + str(value))
    print("===")

The output of the code:

Value: blue
===
Value: square
===
Value: 40
===

Solve ValueError: too many values to unpack (expected 2) Using Dictionary items()

With the first code snippet that gave the ValueError: too many values to unpack (expected 2) error, we can maintain the two variables with the aid of a dictionary method, items() without any error.

The items() method, when we apply it to a dictionary, returns a key-value pair (view object) that is stored within a tuple. With the help of destructuring, we can parse the two values into variables (key and value) and use them within our for loop code block to access the keys and values separately.

dictionary = {"color": "blue", "shape": "square", "volume": 40}

for key, value in dictionary.items():
    print("Key: " + str(key))
    print("Value: " + str(value))
    print("===")

The output of the code:

Key: color
Value: blue
===
Key: shape
Value: square
===
Key: volume
Value: 40
===

To better understand what dictionary.items() outputs, the below code is helpful.

print(dictionary.items())

Output:

dict_items([('color', 'blue'), ('shape', 'square'), ('volume', 40)])

The output is a dict_items list containing tuples we can iterate over.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn

Related Article - Python Error