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

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

php - Using str_replace to translate a MySQL Table in html -

asp.net mvc - Cannot display error message on Editor or EditorFor -