close

[Solved] TypeError: unsupported operand type(s) for +: ‘dict_items’ and ‘dict_items’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: unsupported operand type(s) for +: ‘dict_items’ and ‘dict_items’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: unsupported operand type(s) for +: ‘dict_items’ and ‘dict_items’ Error Occurs?

Today I get the following error TypeError: unsupported operand type(s) for +: ‘dict_items’ and ‘dict_items’ in python.

How To Solve TypeError: unsupported operand type(s) for +: ‘dict_items’ and ‘dict_items’ Error ?

  1. How To Solve TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items' Error ?

    To Solve TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items' Error In python3, dict.items() returns an object with type dict_items which apparently can't be added. (in python 2, it returned a list which could be added).

  2. TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'

    To Solve TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items' Error In python3, dict.items() returns an object with type dict_items which apparently can't be added. (in python 2, it returned a list which could be added).

Solution 1

Another approach to the other suggested answers is:

dict(list(d1.items()) + list(d2.items()))

If there are keys present in both d1 and d2, the value in d2 will be used in the final dictionary.

Solution 2

In python3, dict.items() returns an object with type dict_items which apparently can’t be added. (in python 2, it returned a list which could be added).

An alternative way to add a pair of dictionaries which works on py2k and py3k:

d = dict1.copy()
d.update(dict2)

Of course, there’s some ambiguity about what you want to happen in the case of key collisions. e.g. if both dicts have key1, whose key1 should be preserved in the output? Or should you use some combination of both of their values? In the latter case, you’ll probably want something from the collections module (either defaultdict or Counter)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read