Python字典update()
方法用於將dict2
的鍵值對添加到dict
。此方法不返回任何內容。
語法
以下是update()
方法的語法 -
dict.update(dict2)
參數
- dict2 - 這是要添加到字典
dict
中的字典。
返回值
- 此方法不返回任何值。
示例
以下示例顯示update()
方法的用法 -
#!/usr/bin/python3
dict = {'Name': 'Maxsu', 'Age': 7}
dict2 = {'Sex': 'female' }
dict.update(dict2)
print ("updated dict : ", dict)
當運行上面的程式,它產生以下結果 -
updated dict : {'Sex': 'female', 'Age': 7, 'Name': 'Maxsu'}
上一篇:
Python字典
下一篇:
Python日期和時間