Python字典的items()方法

dict.items() 方法返回键值对组成的新视图。当字典改变时,视图也会相应改变。字典视图可以被迭代

格式及参数

1
dict.items()

dict :字典

实例(3.8.8)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
dict = {
'小明': '20',
'小红': '22',
'小军': '21',
'小刚': '23'
}

print(dict.items())
# dict_items([('小明', '20'), ('小红', '22'), ('小军', '21'), ('小刚', '23')])

for key, value in dict.items():
print(key, value)

'''输出
小明 20
小红 22
小军 21
小刚 23
'''