mdgeus
mdgeus

Reputation: 382

Python dict update is overwriting instead of appending

I'm trying to get a number of products, which are limited by 250 items per call and combine them in one dictionary. The result (per page) is response.json()

itemcount = getProductCount() // 658
print("total products: " + str(itemcount))
pages = math.ceil(itemcount/250) // 3
page =1
print("pages: "+str(pages))
allproducts = {'products': [{}]}
while page <= pages:
    print("page: " +str(page))
    products = getProducts(lang, page, 250)
    allproducts.update(products)
    # for p in products['products']:
    #     print(p['id']) 
    page += 1
print(allproducts)

the outcome is something like this.
So, I can see that my loop is working and the number of pages are correct.
the only thing that is not correct is that allproducts only holds the last 158 items.

total products: 658
pages: 3
page: 1
page: 2
page: 3
{'products': [{'id': 63252188, 'createdAt': '2018-01-11T15:20:16+01:00', 'updatedAt': '2018-04-25T16:35:44+02:00', 'isVisible': True, 'visibility': 'visible', 'hasMatrix': False, 'data01': '', 'data02': '', 'data03': ' etc..

When I print out all p['id'] 's in each loop, I can see that it prints all 658 product id's

What am I missing here? Or should I do it completely different?

Upvotes: 0

Views: 749

Answers (1)

Winson Tanputraman
Winson Tanputraman

Reputation: 3574

I think the problem with the code is that the .update method keeps replacing the value of allproducts['products'], instead of concatenating the existing value with the additional product list.

Instead of

allproducts = {'products': [{}]}
while page <= pages:
    print("page: " +str(page))
    products = getProducts(lang, page, 250)
    allproducts.update(products)

try

allproducts = {'products': []} # modified [{}] to []
while page <= pages:
    print("page: " +str(page))
    products = getProducts(lang, page, 250)
    allproducts['products'].extend(products['products']) # extend instead of update

Upvotes: 1

Related Questions