How does python elegantly append data to an item in an indefinite format dict as an array?

ret = dict()
for symbol in symbols:
    if symbol.basic.name not in ret:
        ret[symbol.basic.name] = [symbol.to_json()]
    else:
        ret[symbol.basic.name].append(symbol.to_json())

this feels so troublesome. Is there any more elegant way to deal with it?

Aug.20,2021

ret.setdefault(symbol.basic.name, []).append(symbol.to_json())
Menu