Skip to content Skip to sidebar Skip to footer

Filter A List Of Dictionary Based On Two Keys

with open('test.csv') as f: list_of_dicts = [{k:v for k, v in row.items()} for row in csv.DictReader(f, skipinitialspace=True)] Hello,I have csv file which I make to a list of

Solution 1:

The easiest way to deduplicate your list of dicts is to build a dictionary keyed by the unique field, which in this case is 'ASIN'. When you find a duplicate, you can select the one with the lower 'Merchant_1_Price' field:

by_asin = {}
for item in list_of_dicts:
    asin = item['ASIN']
    if (
        asinnotin by_asin or
        float(item['Merchant_1_Price']) < float(by_asin[asin]['Merchant_1_Price'])
    ):
        by_asin[asin] = item

deduplicated_list_of_dicts = list(by_asin.values())

In the loop, we're first extracting the asin from the current item since we're going to use it several times. Then we check if that ASIN is either not yet in the by_asin dictionary, or if it is in there, we check if the price on the new item is lower than the price of the old item. In either of those cases, we put the new item into the by_asin dictionary (replacing the previous value, if there was one).

Post a Comment for "Filter A List Of Dictionary Based On Two Keys"