1
我有以下词典示例列表。我想知道为什么下面的代码只会遍历列表中的两个值?为什么它不会遍历列表中的每个值?使用for循环删除列表中的词典
updated_list_of_site_dicts = [{'site': 'living', 'status': 'ready' }, {'site': 'keg', 'status': 'ready' }, {'site': 'box', 'status': 'ready' }, {'site': 'wine', 'status': 'ready' }]
for site_dict in updated_list_of_site_dicts:
if site_dict['status'] == 'ready':
print site_dict['site'] + " is ready"
updated_list_of_site_dicts.remove(site_dict)
print updated_list_of_site_dicts
的可能的复制[为\ Python的_in循环打印列表中的最后一个项目(https://stackoverflow.com/questions/46086441/python-for-in-loop-to-print-the -last-item-in-the-list) –
这也有帮助https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating/1208792#1208792 – Hound