Ways to Solve Cannot Convert Dictionary Update Error

Most of the time we will face an error like cannot convert dictionary update error. Do you know why this happens and how to solve this kind of errors? Before coming to know all about this first we have a clear idea about dictionaries and how to construct the dictionaries. We will learn these things first after that we will move to this error. And we will learn how to rectify this error. Let us start.

Dictionary is an unordered collection of elements. It is useful to store values. It is something different from other data types, that holds only a single value as an element. The dictionary holds a key and value. Both the keys and value are separated by a semicolon, and it is a pair of elements.

Ways to construct a dictionary

We can construct a dictionary in two different ways:

  • Construct from mapping
  • From an iterable of key-value pairs

These are the two different ways to construct a dictionary in two different ways.

Mapping types- dict

A value that maps hashable values to arbitrary objects is called mapping. Dictionary is the only standard mapping type in python. The keys in the dictionary are almost arbitrary values. Values in the dictionary are hashable. Hashable is something that doesn’t change. So we can simply say that values are immutable ones.

Dictionary can be created by a pair of key and values, for example {“jack”:673, “jim”:450}, {673:”jack”,450:”jim”}, or by the dict constructor.

How the dictionaries will be created?

  • It will create an empty dictionary if the positional argument is not specified.
  • If the positional argument is given, it will be considered as a mapping object. And the dictionary is created with the same key-value pair as the mapping objects.
  • Otherwise, the positional argument will be an iterable object.
  • If the key value is declared more than one time means, the value in the new dictionary is the last value for that key.

Examples for dictionary

>>> dict(a=3,b=5,c=6)
{'a': 3, 'b': 5, 'c': 6}
>>> {'a': 3, 'b': 5, 'c': 6}
{'a': 3, 'b': 5, 'c': 6}
>>> dict(zip(['a','b','c'], [3, 5, 6]))
{'a': 3, 'b': 5, 'c': 6}

TypeError:cannot convert dictionary update sequence element #0 to a sequence

This error happens because we are trying to construct a dictionary from an iterable. The first element of the iterable is not a sequence.

Example 1: TypeError

a=dict({1,2,5,9})
print(a)

This is trying to use the first element as a key-value pair. It is not possible so that it raises TypeError. Let us see what will be the output for this program.

Output

Traceback (most recent call last):
  File "C:/Users/AppData/Local/Programs/Python/Python39/dict.py", line 1, in <module>
    a=dict({1,2,5,9})
TypeError: cannot convert dictionary update sequence element #0 to a sequence

Example 1: Solved TypeError

a=dict({1:2,5:9})
print(a)

Instead of using commas, we can use a semicolon, it won’t display any errors. And also it will run properly.

Output

{1: 2, 5: 9}

Example 2: TypeError

def index(string):
    dict={}
    word=-1
    for words in string:
        if words.lower()==words.lower():
            word=word+1
            dict.update({words.upper(),word})
        else:
            word=1
            dict.update({words.upper(),word})
    return dict
print(index('PYthOn'))

The above example will return the index of each word and if the words are in lowercase it will return the word in uppercase. If it is in uppercase letters it will simply display that.

Create a function named index to calculate the index of the string. Create an empty dictionary. Initializing the words as -1. So by incrementing the word it will start from 0. If we start with 0 means the index position will start from 0. As a result, it’ll throw the error. To fix this issue, create a for loop to iterate till the end of the string with a conditional statement.

Let us run this and see what will be the output.

Output

Traceback (most recent call last):
  File "C:\Users\priyalakshmi\AppData\Local\Programs\Python\Python39\wordcount.py", line 27, in <module>
    print(index('PYthOn'))
  File "C:\Users\priyalakshmi\AppData\Local\Programs\Python\Python39\wordcount.py", line 22, in index
    dict.update({words.upper(),word})
TypeError: cannot convert dictionary update sequence element #0 to a sequence

Example 2: Solved TypeError

def index(string):
    dict={}
    word=-1
    for words in string:
        if words.lower()==words.lower():
            word=word+1
            dict.update({words.upper():word})
        else:
            word=1
            dict.update({words.upper():word})
    return dict
print(index('PYthOn'))

The error has happened because of wrong syntax. We have to change the line “dict.update({words.upper(),word}) ” to dict.update({words.upper():word}). Now let us run the code.

Output

{'P': 0, 'Y': 1, 'T': 2, 'H': 3, 'O': 4, 'N': 5}

1. What are the ways to construct a dictionary?

We can construct a dictionary in two different ways, construct from mapping
from an iterable of key-value pairs

2. What is the use of the update() method of dictionary?

update() method in the dictionary is useful to add elements in the dictionary.
Example:
>>>d=({1:2})
>>>d.update({5:9})
>>>d
{1: 2, 5: 9}

3. Does the update() method accepts the list or tuple too?

Yes, we can use any iterable that has key-value pair. As long as you can convert the list/tuple to iterable with the correct correlation between keys and values, you can use the update() method.

Conclusion

In this article, we have completely learned about an error cannot convert dictionary update error. With that, we also learned some additional information about dictionaries. We hope this article will be very much useful for you. Try to learn the article completely. In case of any queries do let us know in the comment section. We will be here to help you.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments