myjson = [
{"GROUP" : "A",
"TYPE" : "I",
"VALUE1" : 25,
"VALUE2" : 26,
"REMARK" : "None"},
{"GROUP" : "B",
"TYPE" : "II",
"VALUE1" : 33,
"VALUE2" : 22,
"REMARK" : "None"}
]
Expected output
[{'GROUP': 'A', 'TYPE': 'I'}, {'GROUP': 'B', 'TYPE': 'II'}]
My approach
For each item in myjson, selecting keys GROUP and TYPE to form a dictionary object, and append these two dictionaries object into another list myjson2
temp = {}
myjson2 = []
for listitem in myjson:
for key, item in listitem.items():
if key == "GROUP" or key == "TYPE":
temp[key] = item
print(temp)
myjson2.append(temp)
The print(temp) above gives:
{'GROUP': 'A', 'TYPE': 'I'}
{'GROUP': 'B', 'TYPE': 'II'}
I wonder why the following results after each .append().
print(myjson2)
[{'GROUP': 'B', 'TYPE': 'II'}, {'GROUP': 'B', 'TYPE': 'II'}]