TypeError: unsupported operand type(s) for +: 'dict_keys' and 'list'

In Python 3.x, dict.keys returns a dictionary view:

>>> a = {1:1, 2:2}
>>> a.keys()
dict_keys([1, 2])
>>> type(a.keys())
<class 'dict_keys'>
>>>

You can get what you want by putting those views in list:

X6_IGNORED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF', 'B']
X9_REMOVED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF']

Actually, you don't even need .keys anymore since iterating over a dictionary yields its keys:

X6_IGNORED = list(X2_X5_MAPPINGS) + ['BN', 'PDF', 'B']
X9_REMOVED = list(X2_X5_MAPPINGS) + ['BN', 'PDF']

Yes, it has something to do with your Python version. In Python 2.x, dict.keys returns a list of a dictionary’s keys. In Python 3.x, it provides a view object of the keys.

You can call list() on the result to make it a list, or just call list() on the entire dictionary as a shortcut.


In Python 3.x, dict.keys doesn't return a list, but instead a view object, dict_keys.

To achieve what you wanted, you need to convert it to a list:

X6_IGNORED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF', 'B']