python - Update dictionary items with a for loop -
i update dictionary items in loop here have:
>>> d = {} >>> in range(0,5): ... d.update({"result": i}) >>> d {'result': 4}
but want d have following items:
{'result': 0,'result': 1,'result': 2,'result': 3,'result': 4}
as mentioned, whole idea of dictionaries have unique keys. can have 'result'
key , list
value, keep appending list.
>>> d = {} >>> in range(0,5): ... d.setdefault('result', []) ... d['result'].append(i) >>> d {'result': [0, 1, 2, 3, 4]}
Comments
Post a Comment