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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -

Why does a .NET 4.0 program produce a system.unauthorizedAccess error on a Windows Server 2012 machine with .NET 4.5 installed? -