I can print listurl when print(list_url) is in the if loop, but I can't print it when it's outside the for loop

50 Views Asked by At
list_url = []
for i in key_products: #must be for i in products_id:
     #sortir url pour bon id
     if keyValue == products_id[i]['files'][0]['type']:
          list_url_2 = i, products_id[i]['files'][0]['url']
     #créer une liste de tupples avec list url
          list_url.append(list_url_2)
          list_url
print(list_url)
#I can't print list_url, type none, like it doesn't exists
list_url = []
for i in key_products:#must be for i in products_id:
     #sortir url pour bon id
     if keyValue == products_id[i]['files'][0]['type']:
          list_url_2 = i, products_id[i]['files'][0]['url']
     #créer une liste de tupples avec list url
          list_url.append(list_url_2)
          list_url
          print(list_url)
#list_url appends correctly, and print correctly with a type list

I expect to retrieve my list outside the for loop, I did it before, but for a reason I ignore, it doesn't work

below, it's an short extraction of the initial Json object I catched: key_products is a list ok keys: ex: 202306098320880 producs_id is a list of dicts, and the value I want to extract with specifics condition are in a dict

#if type = video get url

The URL are fake!! for the examples of json below:

"products": {
        "202306098320880": {
            "vrm": "KV23BYB",
            "dealer_id": "8819",
            "files": [
                {
                    "stock_reference": "202306098320880",
                    "type": "thumbnail",
                    "content_type": "image/jpeg",
                    "url": "https://on.com/repo/202306098320880/1280x720-thumb-play.jpg"
                },
                {
                    "stock_reference": "202306098320880",
                    "type": "poster",
                    "content_type": "image/jpeg",
                    "url": "https://on.com/repo/202306098320880/1280x720.jpg"
                },
                {
                    "stock_reference": "202306098320880",
                    "type": "video",
                    "content_type": "video/mp4",
                    "url": "https://on.com/repo/202306098320880/1280x720.mp4
                }
            ],
            "images": [],
            "on_images": [],
            "youtube": null
        },
        "202309262364380": {
0

There are 0 best solutions below