Issue
I have a list like below -
[{'amount': 1100000, 'target': 120000, time': '2022-09-30T00:00:00.000Z'}, {'amount': 1100000, 'target': 120000, time': '2022-09-30T00:00:00.000Z'},
{'amount': 1100000, 'target': 120000, time': '2022-09-30T00:00:00.000Z'}]
Need to add index key and its value in these three records. Expected output -
[{'amount': 1100000, 'target': 120000, time': '2022-09-30T00:00:00.000Z','index':0}, {'amount': 1100000, 'target': 120000, time': '2022-09-30T00:00:00.000Z','index':1},
{'amount': 1100000, 'target': 120000, time': '2022-09-30T00:00:00.000Z','index':2}]
Please help.
Solution
lst = [
{"amount": 1100000, "target": 120000, "time": "2022-09-30T00:00:00.000Z"},
{"amount": 1100000, "target": 120000, "time": "2022-09-30T00:00:00.000Z"},
{"amount": 1100000, "target": 120000, "time": "2022-09-30T00:00:00.000Z"},
]
for i in range(len(lst)):
lst[i]['index']=i
print(lst)
Output
[{'amount': 1100000, 'target': 120000, 'time': '2022-09-30T00:00:00.000Z', 'index': 0}, {'amount': 1100000, 'target': 120000, 'time': '2022-09-30T00:00:00.000Z', 'index': 1}, {'amount': 1100000, 'target': 120000, 'time': '2022-09-30T00:00:00.000Z', 'index': 2}]
Answered By - joe Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.