+ 1
How to append a value to an existing key in a dictionary without overwriting it?
The key word is "append" here. Instead of overwriting the old value, I want to keep the old value then add a new one. So far, I've tried append the value into a list, then trying to assign the list to the key, but it returns a None value.
2 ответов
+ 2
What do you mean by adding the value? Each key can only have one value, and if you want to store many values, use a list as value.
like:
myDic = {0: ["some data"]}
myDic[0] would return ["some data"]
myDic[0].append("some other data")
myDic[0] would now return ["some data", "some other data"]
myDic[0][0] would return "some data"
0
yeah i dont say that the value has to be multiple string or integers or something. thanks anyway