How To Resolve ValueError: dictionary update sequence element #0 has length N; 2 is required in Python

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

ValueError: dictionary update sequence element #0 has length N; 2 is required in Python. The following is the cause and some ways to fix this error, such as how to pass parameters to the dict.update() function correctly or create a dictionary from the zip() function. Please read the following article.

What causes the ValueError: dictionary update sequence element #0 has length N; 2 is required?

Syntax:

dict.update(dictionary added to the first dictionary)

The dict.update() function returns the dictionary after adding the elements of the second dictionary to the first dictionary.

The error because the parameter you pass to the dict.update() function is incorrect. 

Example: 

firstDict = {'Name': 'John', 'Age': 18}
dictAddInfor = {'Vacations'}

# Use the dict.update to add an element to a dictionary
firstDict.update(dictAddInfor)
print(firstDict)

Output:

Traceback (most recent call last):
 File "./prog.py", line 5, in <module>
ValueError: dictionary update sequence element #0 has length 9; 2 is required

How to solve the ValueError: dictionary update sequence element #0 has length N; 2 is required?

The update() function parameter must be a key-value pair

As in the error example above the variable, ‘dictAddInfor’ contains only dictionary keys. To fix the error you need to add a dictionary value.

Example:

firstDict = {'Name': 'John', 'Age': 18}

# Add dictionary value
dictAddInfor = {'Vacation': 'Student'}

# Use the dict.update to add an element to 'firstDict'
firstDict.update(dictAddInfor)
print(firstDict)

Output:

{'Name': 'John', 'Age': 18, 'Vacation': 'Student'}

Use the zip() function

You can create a dictionary from the zip() function when you have a list or tuple of equal length.

Syntax:

zip(iterable)

Parameter:

  • iterable: Iterables are available in Python as list, string, dictionary, or user-defined.

A list of tuple iterators that mix elements from different iterators make up the zip object that Python’s zip() method delivers (made up of other iterables). 

Example:

  • Two lists of equal length.
  • Use the zip() function to create an iterator as a list of tuples that combine elements from other iterators.
  • The dict() function converts the results obtained from the zip() function to a dictionary.
keysList = ['Name', 'Age', 'Vacations']
valuesList = ['John', 18, 'Student']

# Use the zip() and the dict() function to convert two list to the dictionary
result = dict(zip(keysList, valuesList))
print(result)

Output:

{'Name': 'John', 'Age': 18, 'Vacations': 'Student'}

Summary

Article on how to solve the ValueError: dictionary update sequence element #0 has length N; 2 is required is over. Remember to pass the parameters to the dict.update function correctly. If there are other ways or ideas about the article, please leave a comment. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *