Python - 从字典列表中删除键
有时,在使用Python字典时,我们可能会遇到需要从字典列表中删除特定键的问题。这种问题非常普遍,几乎在所有领域都有应用,包括日常编程和 Web 开发领域。让我们讨论可以执行此任务的某些方式。
方法 #1:使用循环 + del
上述功能的组合可以用来解决这个问题。在此,我们迭代所有键并使用 del 从每个字典中删除所需的键。
Python3
# Python3 code to demonstrate working of
# Remove Key from Dictionary List
# Using loop + del
# initializing list
test_list = [{'Gfg' : 1, 'id' : 2, 'best' : 8 },
{'Gfg' : 4, 'id' : 4, 'best': 10},
{'Gfg' : 4, 'id' : 8, 'best': 11}]
# printing original list
print("The original list is : " + str(test_list))
# initializing key
del_key = 'id'
# Remove Key from Dictionary List
# Using loop + del
for items in test_list:
if del_key in items:
del items[del_key]
# printing result
print("The modified list : " + str(test_list))
Python3
# Python3 code to demonstrate working of
# Remove Key from Dictionary List
# Using list comprehension + dictionary comprehension
# initializing list
test_list = [{'Gfg' : 1, 'id' : 2, 'best' : 8 },
{'Gfg' : 4, 'id' : 4, 'best': 10},
{'Gfg' : 4, 'id' : 8, 'best': 11}]
# printing original list
print("The original list is : " + str(test_list))
# initializing key
del_key = 'id'
# Remove Key from Dictionary List
# Using list comprehension + dictionary comprehension
res = [{key : val for key, val in sub.items() if key != del_key} for sub in test_list]
# printing result
print("The modified list : " + str(res))
输出 :
The original list is : [{'best': 8, 'id': 2, 'Gfg': 1}, {'best': 10, 'id': 4, 'Gfg': 4}, {'best': 11, 'id': 8, 'Gfg': 4}]
The modified list : [{'best': 8, 'Gfg': 1}, {'best': 10, 'Gfg': 4}, {'best': 11, 'Gfg': 4}]
方法#2:使用列表理解+字典理解
这是可以执行此任务的另一种方式。在此,我们重建每个字典,从中删除特定的键。
Python3
# Python3 code to demonstrate working of
# Remove Key from Dictionary List
# Using list comprehension + dictionary comprehension
# initializing list
test_list = [{'Gfg' : 1, 'id' : 2, 'best' : 8 },
{'Gfg' : 4, 'id' : 4, 'best': 10},
{'Gfg' : 4, 'id' : 8, 'best': 11}]
# printing original list
print("The original list is : " + str(test_list))
# initializing key
del_key = 'id'
# Remove Key from Dictionary List
# Using list comprehension + dictionary comprehension
res = [{key : val for key, val in sub.items() if key != del_key} for sub in test_list]
# printing result
print("The modified list : " + str(res))
输出 :
The original list is : [{'best': 8, 'id': 2, 'Gfg': 1}, {'best': 10, 'id': 4, 'Gfg': 4}, {'best': 11, 'id': 8, 'Gfg': 4}]
The modified list : [{'best': 8, 'Gfg': 1}, {'best': 10, 'Gfg': 4}, {'best': 11, 'Gfg': 4}]