Home > Mobile >  Find in a list of dicts all duplicated "sku" and append the quantity of the "sku"
Find in a list of dicts all duplicated "sku" and append the quantity of the "sku"

Time:11-28

I have a list of dicts that looks like this:

totalList = [   
    {'sku': '222222', 'title': 'apple', 'quantity': '2', 'price': '3$'},
    {'sku': '333333', 'title': 'banana', 'quantity': '1', 'price': '1.5$'},
    {'sku': '444444', 'title': 'peach', 'quantity': '5', 'price': '9$'},
    {'sku': '123456', 'title': 'tv', 'quantity': '1', 'price': '500$'},
    {'sku': '777777', 'title': 'apple', 'quantity': '2', 'price': '3$'},
    {'sku': '123456', 'title': 'tv', 'quantity': '2', 'price': '1000$'},
    {'sku': '333333', 'title': 'banana', 'quantity': '4', 'price': '6$'},
]

the final result should look like this:

totalList = [   
    {'sku': '222222', 'title': 'apple', 'quantity': '2', 'price': '3$'},
    {'sku': '333333', 'title': 'banana', 'quantity': '5', 'price': '7.5$'},
    {'sku': '444444', 'title': 'peach', 'quantity': '5', 'price': '9$'},
    {'sku': '123456', 'title': 'tv', 'quantity': '3', 'price': '1500$'},
    {'sku': '777777', 'title': 'apple', 'quantity': '2', 'price': '3$'},
]

my code so far is looking like this:

newList = []
    for x in totalList:
        for y in totalList:
            if x['sku'] == y['sku']:
                x['quantity'] = int(x['quantity'])   int(y['quantity'])
            else:
                newList.append(x)

it should find all duplicated "sku" and then calc them all together into 1 remove all other duplicates and have like a summary of everything in 1 list.

CodePudding user response:

Try:

totalList = [
    {"sku": "222222", "title": "apple", "quantity": "2", "price": "3$"},
    {"sku": "333333", "title": "banana", "quantity": "1", "price": "1.5$"},
    {"sku": "444444", "title": "peach", "quantity": "5", "price": "9$"},
    {"sku": "123456", "title": "tv", "quantity": "1", "price": "500$"},
    {"sku": "777777", "title": "apple", "quantity": "2", "price": "3$"},
    {"sku": "123456", "title": "tv", "quantity": "2", "price": "1000$"},
    {"sku": "333333", "title": "banana", "quantity": "4", "price": "6$"},
]

out = {}
for d in totalList:
    out.setdefault(d["sku"], []).append(d)

out = [
    {
        "sku": k,
        "title": v[0]["title"],
        "quantity": str(sum(int(d["quantity"]) for d in v)),
        "price": str(sum(float(d["price"][:-1]) for d in v))   "$",
    }
    for k, v in out.items()
]

print(out)

Prints:

[
    {"sku": "222222", "title": "apple", "quantity": "2", "price": "3.0$"},
    {"sku": "333333", "title": "banana", "quantity": "5", "price": "7.5$"},
    {"sku": "444444", "title": "peach", "quantity": "5", "price": "9.0$"},
    {"sku": "123456", "title": "tv", "quantity": "3", "price": "1500.0$"},
    {"sku": "777777", "title": "apple", "quantity": "2", "price": "3.0$"},
]

CodePudding user response:

This can be achieved by creating an additional unique list to identify the SKU values as follows:

totalList = [   
    {'sku': '222222', 'title': 'apple', 'quantity': '2', 'price': '3$'},
    {'sku': '333333', 'title': 'banana', 'quantity': '1', 'price': '1.5$'},
    {'sku': '444444', 'title': 'peach', 'quantity': '5', 'price': '9$'},
    {'sku': '123456', 'title': 'tv', 'quantity': '1', 'price': '500$'},
    {'sku': '777777', 'title': 'apple', 'quantity': '2', 'price': '3$'},
    {'sku': '123456', 'title': 'tv', 'quantity': '2', 'price': '1000$'},
    {'sku': '333333', 'title': 'banana', 'quantity': '4', 'price': '6$'}
]



new_list = []
unique_list = []
for sku in totalList:
    if sku['sku'] in unique_list:
        print(sku['sku'], 'already in, so append')
        i = unique_list.index(sku['sku'])
        new_list[i]['quantity']  = sku['quantity']
        new_list[i]['price']  = sku['price']


    else:
        unique_list.append(sku['sku'])
        new_list.append(sku)

print('unique list:', unique_list)
print(new_list)

And the result would be this:

123456 already in, so append
333333 already in, so append
unique list: ['222222', '333333', '444444', '123456', '777777']

[{'sku': '222222', 'title': 'apple', 'quantity': '2', 'price': '3$'}, 
{'sku': '333333', 'title': 'banana', 'quantity': '14', 'price': '1.5$6$'},
 {'sku': '444444', 'title': 'peach', 'quantity': '5', 'price': '9$'}, 
{'sku': '123456', 'title': 'tv', 'quantity': '12', 'price': '500$1000$'},
 {'sku': '777777', 'title': 'apple', 'quantity': '2', 'price': '3$'}]

you can shorten / optimise the code, but i have left the extra print statements in for clarity...

  • Related