I tried my best. But I really need a litte help here 🙂
So, if the code AND land are the same then add this dict part to final.
Next if the next dict part has the same code and land,but different size and value as the previous one, then add size and value to the previous one,while every other part of the dict stays the same.
If code AND land are not the same then skip and add as a new dict to final.
So, in the end it should look like this:
mylist = [{
“size”: 1,
“code”: “300”,
“content”: “xyz”,
“land”: “AB”,
“value”: 5
},
{
"size": 1,
"code": "300",
"name": "xyz",
"land": "AB",
"value": 10
},
{
"size": 1,
"code": "400",
"content": "abc",
"land": "KK",
"value": 12
}]
final = [
{
“size”: 2, #size changed
“code”: “330”,
“content”: “xyz”,
“land”: “AB”,
“value”: 15 #value changed
},
{
"size": 1,
"code": "400",
"content": "abc",
"land": "KK",
"value": 12
}
]
I tried this code. But it just adds the dicts
My code so far:
import json
mylist = [{
"size": 1,
"code": "300",
"content": "xyz",
"land": "AB",
"value": 5
},
{
"size": 1,
"code": "300",
"name": "xyz",
"land": "AB",
"value": 10
},
{
"size": 1,
"code": "400",
"content": "abc",
"land": "KK",
"value": 12
}]
final = []
for i in range(len(mylist)):
if mylist[i]["code"] == "300" and mylist[i]["land"] == "AB":
# here should be logical decision
final.append(mylist[i])
elif mylist[i]["code"] == "400" and mylist[i]["land"] == "KK":
final.append(mylist[i])
else:
pass
with open('stackoverflowResponse.json', 'w') as resp:
json.dump(final, resp, indent=2)