code show as below:
# -*- coding:gb2312 -*-
infor = [{"name":"laowang","age":18},{"name":"laoli","age":20},{"name":"laotie","age":40},{"name":"wutianlong","age":29}]
for temp in infor:
for temp2 in temp.key():
print(temp2)
The running results are as follows:
My question:
I don’t know what the problem is with this code? I have 4 dictionaries in a list. I want to traverse the list once, then traverse the name key of the dictionary again in the traversal result, and then print the value of the name key. Why can't it be printed?
keys
1. First of all, it is
keys
, notkey
2. Secondly,
for
can traverse the dictionary directly without callingkes()
The first floor is highlighted. Keys are the attributes of dict. The error "Dictionary object does not have an attribute called 'key'" has already explained the problem. At this time, you can find the answer by checking the dict index in the Python documentation.