Select Page

How to Solve Python ValueError: dictionary update sequence element #0 has length N; 2 is required

by | Programming, Python, Tips

This error occurs when you try to update a dictionary with incorrect syntax. You can solve this error by updating the dictionary with another dictionary or an iterable object containing key-value pairs. For example,

my_dict = {'name':'Tia', 'subject':'mathematics'}

my_dict.update({'name':'Sil', 'subject':'chemistry'}

The error can also occur when trying to convert a list to a dictionary using the built-in dict() function, you can solve this error by ensuring the list contains tuples with two elements (key-value pairs). For example,

my_lst = [('name','pepperoni'), ('price',9.99)]
my_dict = dict(my_lst)

This tutorial will go through how to solve the error with code examples.


ValueError: dictionary update sequence element #0 has length N; 2 is required

In Python, a value is the information stored within a particular object. We will encounter a ValueError in Python when we use an operation or function that receives an argument with the right type but an inappropriate value.

The update() method inserts specified items from a dictionary or an iterable object with key-value pairs. Passing an iterable to the update() method is an appropriate type but if the iterable does not contain key-value pairs, then it has an inappropriate value, raising the ValueError.

Example #1: Updating a dictionary using update()

Let’s look at an example of incorrectly using the update() method.

my_dict = {'name':'margherita', 'price':7.99}

my_str = 'pepperoni'

my_dict.update(my_str)

In the above code, we defined a dictionary and a string and then called the update() method to update the dictionary with the string.

Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [6], in <cell line: 3>()
      1 my_dict = {'name':'margherita', 'price':7.99}
      2 my_str = 'pepperoni'
----> 3 my_dict.update(my_str)

ValueError: dictionary update sequence element #0 has length 1; 2 is required

The error occurs because we passed a string instead of an iterable of key-value pairs to the update() method.

Solution #1

We can solve this error by using passing a dictionary to the update() method. We define a dictionary with curly brackets {}. Each key-value pair has a colon between them and pairs are separated by commas. Let’s look at the updated code:

my_dict = {'name':'margherita', 'price':7.99}

my_str = {'name':'pepperoni', 'price':9.99}

my_dict.update(my_str)

print(my_dict)

Let’s run the code to get the updated dictionary.

{'name': 'pepperoni', 'price': 9.99}

Solution #2

We can also solve this error by passing a list of tuples, where each tuple contains two elements (key and value). Let’s look at the revised code:

my_dict = {'name':'margherita', 'price':7.99}

my_lst = [('name','pepperoni'), ('price',9.99)]

my_dict.update(my_lst)

print(my_dict)

Let’s run the code to get the updated dictionary.

{'name': 'pepperoni', 'price': 9.99}

Example #2: Converting a list to a dictionary using dict()

Let’s look at an example of converting a list to a dictionary:

my_lst = ['name']

my_dict = dict(my_lst)

print(my_dict)

In the above code we defined a list and then called the built-in dict() method to convert the list to a dictionary. Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [10], in <cell line: 2>()
      1 my_lst = ['name']
----> 2 my_dict = dict(my_lst)
      3 print(my_dict)

ValueError: dictionary update sequence element #0 has length 4; 2 is required

The error occurs because the dict() function requires keyword arguments (key=value) separated by commas.

Solution #1

We can solve this error by using the zip() function to return an iterator of tuples, which we can convert into a dictionary. We will need to have one list of keys and another list of values to pass to the zip() function. Let’s look at the revised code.

keys = ['name', 'price']

vals = ['pepperoni', 9.99]

my_dict = dict(zip(keys, vals))

print(my_dict)

Let’s run the code to get the dictionary:

{'name': 'pepperoni', 'price': 9.99}

Solution #2

We can also solve the error by passing a list of tuples with two elements to the dict() function. Let’s look at the revised code:

my_lst = [('name','pepperoni'), ('price',9.99)]
my_dict = dict(my_lst)

Let’s run the code to see the dictionary:

{'name': 'pepperoni', 'price': 9.99}

Summary

Congratulations on reading to the end of this tutorial!

For further reading on Python ValueErrors, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!