Skip to content
Advertisement

Python: How do you remove a dict in a list of dicts of list of dicts? [closed]

as a startingpoint i have this list:

product = [
    {'pid': '5678', 'stocktotal':'498', 'variantIds':[{'vid':'1', 'stockQuantity':0},{'vid':'2', 'stockQuantity':199},{'vid':'3', 'stockQuantity':299}]},
    {'pid': '1234', 'stocktotal':'100', 'variantIds':[{'vid':'4', 'stockQuantity':0},{'vid':'5', 'stockQuantity':100},{'vid':'6', 'stockQuantity':0}]}  
    ]

how do you query this list of dicts to remove specific dicts, e.g. when stockQuantity == 0 i want to remove the whole dict, i also want to remove the whole ‘pid’-dict when stocktotal == 0:

product = [
    {'pid': '5678', 'stocktotal':'498', 'variantIds':[{'vid':'2', 'stockQuantity':199},{'vid':'3', 'stockQuantity':299}]},
    {'pid': '1234', 'stocktotal':'0', 'variantIds':[{'vid':'5', 'stockQuantity':100},]}  
    ]

do you do this kind of data cleaning before, while or after you write a CSV File of it?

Advertisement

Answer

I went with a dedication function to perform the filters that you want:

  • Filter out any products.product.variantIds where stockQuantity == 0
  • Calculate the products.product.totalquantity and remove the product from the list if 0
product = [
    {'pid': '5678', 'stocktotal':'498', 'variantIds':[{'vid':'1', 'stockQuantity':0},{'vid':'2', 'stockQuantity':199},{'vid':'3', 'stockQuantity':299}]},
    {'pid': '1234', 'stocktotal':'100', 'variantIds':[{'vid':'4', 'stockQuantity':0},{'vid':'5', 'stockQuantity':100},{'vid':'6', 'stockQuantity':0}]},  
    {'pid': '12345', 'stocktotal':'100', 'variantIds':[{'vid':'4', 'stockQuantity':0},{'vid':'5', 'stockQuantity':0},{'vid':'6', 'stockQuantity':0}]}  
    ]

def remove_stockQuantity_of_zero(products):
    new_list = []
    for item in products:
        new_dict = {}
        for k, v in item.items():
            if k == "variantIds":
                v = list(filter(lambda x: x["stockQuantity"] != 0, v))
            new_dict.update({k:v})
        new_dict["stocktotal"] = sum([x["stockQuantity"] for x in new_dict["variantIds"]])
        if new_dict["stocktotal"] == 0:
            continue
        new_list.append(new_dict)

    return new_list

print(remove_stockQuantity_of_zero(product))
# > [{'pid': '5678', 'stocktotal': 498, 'variantIds': [{'vid': '2', 'stockQuantity': 199}, {'vid': '3', 'stockQuantity': 299}]}, {'pid': '1234', 'stocktotal': 100, 'variantIds': [{'vid': '5', 'stockQuantity': 100}]}]

If you want to store the result in a csv file, then you certainly need to perform this action before writing it to the file.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement