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

avatar

Borislav Hadzhiev

Last updated: Apr 24, 2022

banner

Photo from Unsplash

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

The Python "ValueError: dictionary update sequence element #0 has length N; 2 is required" occurs when we use the dict.update() method incorrectly. To solve the error, pass another dictionary or an iterable of key/value pairs to the dict.update() method.

valueerror dictionary update sequence element 0 has length

Here is an example of how the error occurs.

main.py
my_list = ['name'] my_dict = {'name': 'Alice'} # ⛔️ ValueError: dictionary update sequence element #0 has length 4; 2 is required my_dict.update(my_list)

The dict.update() method can be called with a dictionary or an iterable of key/value pairs (e.g. a list of tuples containing 2 elements).

main.py
my_dict = {'name': 'Alice'} # ✅ pass dictionary to the update() method my_dict.update({'name': 'Bob', 'age': 30}) print(my_dict) # 👉️ {'name': 'Bob', 'age': 30}

The example above passes a dictionary argument to the update() method.

The method can also be called with an iterable of key/value pairs.

main.py
my_dict = {'name': 'Alice', 'age': 29} my_list = [('name', 'Bob'), ('age', 30)] my_dict.update(my_list) print(my_dict) # 👉️ {'name': 'Bob', 'age': 30}

We passed a list of tuples to the dict.update() method. Each tuple must contain 2 elements - a key and a value.

If you have 2 iterables (e.g. lists or tuples) of equal length and need to create a dictionary, use the zip() function.

main.py
my_keys = ['name', 'age'] my_values = ['Alice', 29] my_dict = dict(zip(my_keys, my_values)) print(my_dict) # 👉️ {'name': 'Alice', 'age': 29}

The zip function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The zip function returns an iterator of tuples.

The error is also caused when we try to convert a bad value to a dictionary.

main.py
my_str = 'hello' # ⛔️ ValueError: dictionary update sequence element #0 has length 1; 2 is required my_dict = dict(my_str)

Here are all the different ways to create a dictionary.

main.py
a = dict(name='Alice', age=29) b = {'name': 'Alice', 'age': 29} c = dict(zip(['name', 'age'], ['Alice', 29])) d = dict([('name', 'Alice'), ('age', 29)]) e = dict({'name': 'Alice', 'age': 29}) f = dict({'name': 'Alice'}, age=29) print(a == b == c == d == e == f) # 👉️ True

All of the examples create the same dictionary - {'name': 'Alice', 'age': 29}.

The dict.update method updates the dictionary with the key/value pairs from the provided value.

The method overrides the dictionary's existing keys and returns None.

The dict.update() method can either be called with another dictionary or an iterable of key/value pairs (e.g. a list of tuples with 2 elements each).

You can also pass keyword arguments to the dict.update() method.

main.py
my_dict = dict(name='Alice', age=29) my_dict.update(name='Bob', age=30) print(my_dict) # 👉️ {'name': 'Bob', 'age': 30}

Conclusion #

The Python "ValueError: dictionary update sequence element #0 has length N; 2 is required" occurs when we use the dict.update() method incorrectly. To solve the error, pass another dictionary or an iterable of key/value pairs to the dict.update() method.

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.