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
CodePudding user response:
"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
CodePudding user response:
You can chain dictionary keys to access nested items
all_product=my_dict['commandes'][0]
for one_ligne in all_product['lignes']:
product = one_ligne['product']
# my code
or
for one_ligne in my_dict['commandes'][0]['lignes']:
product = one_ligne['product']
# my code