Trying to print every nth element in strange data format that is identified as list

Viewed 51

I am pulling data via an API that python identifies as a list, but does not seem to act like one.

This is a sample of the data returned:

[{'product_id': '41111', 'type': 'configurable', 'set': '4', 'sku': 'CM5555-CONF', 'position': '1'}, {'product_id': '42222', 'type': 'configurable', 'set': '4', 'sku': 'CM6666-CONF', 'position': '1'}

This is in a varaiable called temp.

When I do print(type(temp)), <class 'list'> is returned.

I am trying to access a list of all product IDs, so they can be fed to a different request to the API.

When I do print(temp[::1]), the entire list (or whatever it is) is returned, as though I just did print(temp) by itself.

How can I print just a list of product_ids by themselves?

1 Answers

You are correct that it is a list but inside the list are {} which are dictionaries. You have to reference their key in order to pull them out. Try:

for datadict in temp:
    print(datadict['product_id'])
Related