Remove duplicates from a list of dictionaries when only one of the key values is different

前端 未结 5 1095
误落风尘
误落风尘 2020-12-12 02:37

I have seen some similar answers, but I can\'t find something specific for this case:

I have a list of dictionaries like this:

[
 {\"element\":Bla, \         


        
5条回答
  •  时光说笑
    2020-12-12 03:06

    Apologies for terrible variable names. There is probably a cleaner way but this should work

    seen = {(item["element"], item["version"]): False for item in mylist}
    
    output = []
    for item in mylist:
        item_key = (item["element"], item["version"])
        if not seen[item_key]:
            output.append(item)
            seen[item_key] = True
    

提交回复
热议问题