user1943313
user1943313

Reputation: 33

How to iterate through this dictionary

I am using a python shopping cart that stores my product's options in a list of dictionaries. When I log the options object to see its structure, it is stored like this:

[[{'option': <model.Option object at 0xfc6cd6b0>}, {'option': <model.Option object at 0xfc6cd7b0>}]]

This is running on Google App Engine, and for each option selected the options model object is stored in the dictionary.

My intent is to loop through this structure and save <model.Option object at 0xfc6cd6b0> and <model.Option object at 0xfc6cd7b0> to the datastore, but I am unable to figure out how to do this. Can somebody show me how to do this?

Upvotes: 3

Views: 157

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122322

It is a nested list. Unwrap the outer list, then iterate over the inner, taking out the option value:

for d in yourlst[0]:
    option = d['option']
    # do something with option

You can also combine that into a generator expression:

for option in (d['option'] for d in yourlst[0]):
    # do something with option

Upvotes: 3

Related Questions