Thanks to visit codestin.com
Credit goes to github.com

Skip to content

dict.update() does not accept a dict as input #647

Closed
@kimbauters

Description

@kimbauters

It is not possible to provide a dict as the input of a dict.update() method, for example:

d1 = {1:'a'}
d2 = {2:'b'}

d1.update(d2)

The exact error will vary depending on the type of the key. In this case, the error "'int' object is not iterable" is thrown. If the key was a string, then the error "ValueError: dictionary update sequence has the wrong length" is thrown. Both together make it clear that it tries to iterate the key object, looking for a list of pairs. This code does work correctly in Python 3.

A common way around this issue is to use the Python 2 style:

d3 = (d1, **d2)

This does not work in Python 3 for the given example, but it silently fails in micropython. Instead of throwing an error, the result of d3 will simply be d1. It would work in Python 3 if the keys were strings. However, again, in that case in micropython the result is simply that d3 will be the same as d1.

Only other solution I can think of for the moment is to just iterate over all items of the second dictionary and add them to the first dictionary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions