Python - 新增字典項
新增項
透過使用新的索引鍵併為其賦值來向字典新增項。
示例
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
自己動手試一試 »
更新字典
update()
方法將使用給定引數中的項來更新字典。如果該項不存在,則會新增該項。
引數必須是一個字典,或者是一個帶有鍵值對的可迭代物件。
示例
使用 update()
方法向字典新增一個顏色項
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"color": "red"})
自己動手試一試 »