如何从字典列表中删除列表中的每个值的项目



我试图返回字典列表,除了字典与game_list_id相同的id

这是我到目前为止得到的:

list_games = [
{'id': 10, 'name_game': 'x1'},
{'id': 20, 'name_game': 'x2'},
{'id': 30, 'name_game': 'x3'},
{'id': 40, 'name_game': 'x4'},
{'id': 50, 'name_game': 'x5'},
{'id': 60, 'name_game': 'x6'},
{'id': 70, 'name_game': 'x7'},
{'id': 80, 'name_game': 'x8'},
{'id': 90, 'name_game': 'x9'}
]
game_id_list = [10, 30, 40]

def remove_game_id_list(list_games, game_id_list):
results = []
for b in game_id_list:
for i in range(len(list_games)):
if list_games[i]['id'] != b:
results.append(list_games[i])
return results

您可以在单行中完成,而不需要函数:

result = [dct for dct in list_games if dct['id'] not in game_id_list]

相关内容

  • 没有找到相关文章

最新更新