-4

the dictionary on itself it has unique keys, but if I'm appending the dictionaries into a list, every dictionary is being separated with its unique values and it may be duplicated keys, every key in the separate dictionary into the list,

in my case I'm working product summary report for POS orders, I make a method that loop over all pos order lines and retrieve data from there, the final result should be unique product name with the sum of sold quantities to this product overall orders, I did it, but the output returns non-unique products and the length of the list is the summation of the order lines, I need to remove all duplicated product and set only the last one which has the maximum quantity, here is my code

    @api.multi
    def product_summary_test(self):
        product_summary_dict = {}
        data = []
        if self.date_from and self.date_to:
            order_detail = self.env['pos.order'].search([('date_order', '>=', self.date_from),
                                                         ('date_order', '<=', self.date_to)])
            if order_detail:
                for each_order in order_detail:
                    for each_order_line in each_order.lines:
                        if each_order_line.product_id.name in product_summary_dict:
                            product_qty = product_summary_dict[each_order_line.product_id.name]
                            product_qty += each_order_line.qty
                            res1 = {
                                "name": each_order_line.product_id.name,
                                "sold_qty": product_qty,
                            }
                            data.append(res1)
                        else:
                            product_qty = each_order_line.qty
                            res2 = {
                                "name": each_order_line.product_id.name,
                                "sold_qty": product_qty,
                            }
                            data.append(res2)
                        product_summary_dict[each_order_line.product_id.name] = product_qty;
        if data:
            print(len(data))
            print(data)
            return data
        else:
            return {}

the output be like

[{'name': 'x', 'sold_qty': 2.0}, {'name': 'x', 'sold_qty': 8.0},{'name': 'x', 'sold_qty': 12.0}, {'name': 'y', 'sold_qty': 5.0}, {'name': 'y', 'sold_qty': 7.0, {'name': 'y', 'sold_qty': 9.0}]

its overwrite the same product and add the new quantity in sold_qty 2 + 6 + 4 as in x

it should be just :

[{'name': 'x', 'sold_qty': 12.0},{'name': 'y', 'sold_qty': 9.0}]

how that can be done ?!

thanks in advance

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
Mohamed Fouad
  • 476
  • 9
  • 27
  • 2
    [Remove duplicates from list of dicts](https://stackoverflow.com/q/41704229/674039) – wim Jul 10 '20 at 02:31
  • I need to make it unique for specific key, not all keys because the is a lot of products have the same sold quantity – Mohamed Fouad Jul 10 '20 at 02:49
  • 1
    Does this answer your question? [Remove duplicates from list of dicts](https://stackoverflow.com/questions/41704229/remove-duplicates-from-list-of-dicts) – Red Jul 11 '20 at 15:16

2 Answers2

1

You can use this

unique_list = list(my_dic_data['key'].unique())

Imran
  • 775
  • 7
  • 19
0

I did it in this way

if data:
   datas =list({v['name']: v for v in data}.values())
   print(len(datas))
   print(data)
else:
   return {}

and its worked well

Mohamed Fouad
  • 476
  • 9
  • 27