0

I want to generate a list that contains several dictionaries, but I encountered a weird problem:

for i in xrange(0, len(merchant_id_list)):
        for j in xrange(count, count + int(product_list_size[i])):
            unit_price = get_unit_price_by_id(merchant_id_list[i], product_id_list[j])

            product['id'] = product_id_list[j]
            product['quantity'] = quantity_list[j]
            product['price'] = unit_price
            product_id.append(product)

        print i
        print merchant_id_list[i]
        compound_data['id'] = merchant_id_list[i]
        compound_data['product_id'] = product_id
        merchant.append(compound_data)
        print merchant

        count += int(product_list_size[i])

I want to get the list looks like this:

[
                {
                    "id":1,
                    "product_id":[
                        {
                            "id":1,
                            "quantity": 3,
                            "price": 11
                        },
                        {
                            "id":2,
                            "quantity": 2,
                            "price": 12
                        }
                    ]

                },
                {
                    "id":2,
                    "product_id":[
                        {
                            "id":16,
                            "quantity": 2,
                            "price": 22.22
                        }
                    ]

But finally it looks like this:

[
                {
                    "id":2,
                    "product_id":[
                        {
                            "id":16,
                            "quantity": 2,
                            "price": 22.22
                        },
                {
                    "id":2,
                    "product_id":[
                        {
                            "id":16,
                            "quantity": 2,
                            "price": 22.22
                        }
                    ]

It looks that the last element override all previous dictionary. I've print the variable 'i' and 'merchant_id_list' and it is OK. How to fix it? Thanks

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
user1179442
  • 473
  • 5
  • 16
  • 1
    The marked question is the same problem you are having - it does not matter whether you are storing sub-lists or dictionaries in your list, just that it's some kind of mutable object. The solution will be different for you, but the principle is the same - you must create a *separate object* for each thing you want to put in the list. – Karl Knechtel Nov 09 '14 at 08:15

1 Answers1

0

Instead of re-using the same dictionary over and over, just create a new product dictionary on each iteration:

for i in xrange(0, len(merchant_id_list)):
    for j in xrange(count, count + int(product_list_size[i])):
        unit_price = get_unit_price_by_id(merchant_id_list[i], product_id_list[j])

        product = {}                          # <-- create a new product dictionary
        product['id'] = product_id_list[j]
        product['quantity'] = quantity_list[j]
        product['price'] = unit_price
        product_id.append(product)
        ...
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485