Python 字典 items() 方法

實例

返回字典的鍵值對:

car = {
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
x = car.items()
print(x)

運行實例

定義和用法

items() 方法返回一個 view 對象。這個視圖對象包含字典的鍵值對,形式為列表中的元組。

視圖對象將反映對字典所做的任何更改,請看下面的例子。

語法

dictionary.items()

參數值

無參數

更多實例

實例

當字典中的項目值發生改變時,視圖對象也會更新:

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = car.items()
car["year"] = 2018
print(x)

運行實例