Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate through a complex dictionary with python

I would like to browse this dictionary especially the lines sub-array

my_dict= {
  "alla": "koko",
  "messages": [
  {
    "env": "dlo",
    "detail": "cons"
  }
],
"commandes": [
{
  "comande_numero": "lkm02",
  "date": "14/10/2022",
  "lignes": [
    {
      "product": "mango",
      "designation": "04 pacquets of mango",
      "quantite": 14
    },
    ......
    ]
  }
 ]
}

I tried

all_product=my_dict['commandes']
for one_ligne in all_product.lignes:
    // my code

but i have an error,
So how to browse the rows sub-array located at the dictionary level

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"commandes" is a list, not a dictionary, so iterate properly:

all_product=my_dict['commandes']
for product in all_product:
    for ligne in product["lignes"]:
        // your code
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading