Python - How to add values to a key with existing

2020-03-30 01:35发布

First of all I'm a beginner in Python

I have this dictionary:

d={'Name': ('John', 'Mike'),
   'Address': ('LA', 'NY')}

Now I want to add more values in the keys like this.

d={'Name': ('John', 'Mike', 'NewName'),
   'Address': ('LA', 'NY', 'NewAddr')}

I tried update and append but I think it just works in list / tuples, and also I tried putting it in a list using d.items() and then overwriting the d dictionary but I think its messy and unnecessary?

Is there a direct method for python for doing this?

7条回答
叛逆
2楼-- · 2020-03-30 02:08

Simply add a tuple to existing value

d={'Name': ('John', 'Mike'),
  'Address': ('LA', 'NY')}

d["Name"]=d["Name"]+("lol",)
print d
查看更多
登录 后发表回答