I have a data in list
PRODUCT = [
{
"type": "PLAN A",
"detail": "ABC",
"plans": [
{
"name": 'Monthly',
"id": "",
},
{
"name": 'Annually',
"id": "",
}
],
}, {
"type": 'PLAN B',
"detail": 'XYZ',
"plans": [{
"name": 'Tri-Month',
"id": "",
},
],
}]
I want to loop or iterate over this array and want to update id for example when in plans name is Monthly I want the id to be "abcxyz" when name is Annually I want the id to be "dvgssd" when the name is Tri-Month I want the id to be "abc123.
This is the initial list it has more data in it. I have tried multiple ways to achieve it but I get error.
>Solution :
It is a nested loop problem. The first thing you need to understand is there will be 2 different loops that will be used.
- For the product array.
- For the plans array.
In the first loop, you will get an object from the plans array, and in the second loop, you will set the ids.
Below is the code to help you understand.
for iterator in PRODUCT:
for plan in iterator["plans"]:
if plan["name"] == "Monthly":
plan["id"] = "abcxyz"
if plan["name"] == "Annually":
plan["id"] = "dvgssd"
if plan["name"] == "Tri-Month":
plan["id"] = "abc123"