Nested for loop in Python to generate a dict

I want to iterate over all items in items and over all bands in bands (in the item object). But only the outer for loop works and just for the first item. Any ideas why?

from satsearch import Search
from IPython.display import JSON
import json

# configuration
url = 'https://earth-search.aws.element84.com/v0' # URL to Sentinel 2 AWS catalog
collection = 'sentinel-s2-l2a-cogs'

# search parameter
startDate = '2021-01-01'
endDate = '2021-02-01'
location = [ 11.756057739257812,
          57.649809962218995,
          12.10693359375,
          57.751442372568924
       ]

bbox_search = Search(
    bbox=location, 
    datetime=startDate+"/"+endDate, 
    query={'eo:cloud_cover': {'lt': 50}},
    collections=[collection],
    url=url,
    sort={'field': 'eo:cloud_cover', 'direction': 'desc'},
    )

def get_all_files(*bands):
    bbox_search = Search(
        bbox=location, 
        datetime=startDate+"/"+endDate, 
        query={'eo:cloud_cover': {'lt': 50}},
        collections=[collection],
        url=url,
        sort={'field': 'eo:cloud_cover', 'direction': 'desc'},
    )

    items = bbox_search.items()

    downloads = {}
    data = {}
    
    for i, item in enumerate(items):
        
        data['Product ID']= item.properties["sentinel:product_id"]
        data['Preview']= item.asset("thumbnail")["href"]
        data['Date']= item.properties["datetime"]
        
        for idx, band in enumerate(bands):
            data[band] = item.asset(band)["href"]
        
        downloads[i] = data
                            
    return downloads

display(JSON(get_all_files("B01", "B02", "B03")))

Result: The result is, that only the first (outer) for loop works and just for the first item. The inner for loop will be ignored.

enter image description here

Result after update: enter image description here

Input data: This are the input data: https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items

Answer

This is the perfect working code:

from satsearch import Search
from IPython.display import JSON
import json

# configuration
url = 'https://earth-search.aws.element84.com/v0' # URL to Sentinel 2 AWS catalog
collection = 'sentinel-s2-l2a-cogs'

# search parameter
startDate = '2021-01-01'
endDate = '2021-02-01'
location = [ 11.756057739257812,
          57.649809962218995,
          12.10693359375,
          57.751442372568924
       ]

bbox_search = Search(
    bbox=location, 
    datetime=startDate+"/"+endDate, 
    query={'eo:cloud_cover': {'lt': 50}},
    collections=[collection],
    url=url,
    sort={'field': 'eo:cloud_cover', 'direction': 'desc'},
    )

def get_all_files(*bands):
    bbox_search = Search(
        bbox=location, 
        datetime=startDate+"/"+endDate, 
        query={'eo:cloud_cover': {'lt': 50}},
        collections=[collection],
        url=url,
        sort={'field': 'eo:cloud_cover', 'direction': 'desc'},
    )

    items = bbox_search.items()

    downloads = {}
    
    for i, item in enumerate(items):

        data = {}
        
        data['Product ID']= item.properties["sentinel:product_id"]
        data['Preview']= item.asset("thumbnail")["href"]
        data['Date']= item.properties["datetime"]
        
        for idx, band in enumerate(bands):
            data[band] = item.asset(band)["href"]
        
        downloads[i] = data
                            
    return downloads

display(JSON(get_all_files("B01", "B02", "B03")))

What I’ve changed (thanks to the comments):

  • I used i in both loops. Changed the inner loop this way for idx, band in enumerate(bands): helps.
  • Adding the key, value pairs from the inner loop to the data dict, too.