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