How to use python: continuously

inverse.setdefault(val, []).append(key)

what function does this sentence achieve? it is easy to understand using one method, but using two methods in a row. I don"t understand how the value is passed.

the following is the complete code

def invert_dict(d):
    inverse = {}
    for key in d:
        val = d[key]
        inverse.setdefault(val, []).append(key)
    return inverse
Mar.02,2021

inverse.setdefault (val, []) .append (key)

it is equivalent to:

>>> d = {}
>>> d.setdefault(1, []).append(2)
>>> print(d)
{1: [2]}
Menu