List generated from For loop with .append() of another JSON data
Solution 1:
Why not working: You need to append the new dictionary every time instead of overwriting the old one with a new value. Just move that temp inside the first for
,
myjson = [
{
"GROUP": "A",
"TYPE": "I",
"VALUE1": 25,
"VALUE2": 26,
"REMARK": "None"
},
{
"GROUP": "B",
"TYPE": "II",
"VALUE1": 33,
"VALUE2": 22,
"REMARK": "None"
}
]
myjson2 = []
for listitem in myjson:
temp = {}
for key, item in listitem.items():
if key == "GROUP" or key == "TYPE":
temp[key] = item
myjson2.append(temp)
print(myjson2)
One-Liner: with list comprehension
myjson2 = [{key:val for key, val in dic.items() if key in ['GROUP', 'TYPE']} for dic in myjson]
Solution 2:
Your issue is caused by temp
being defined once outside the loops.
Move the definition inside the first for
so we clear it for each item in the list:
myjson2 = []
for listitem in myjson:
temp = {}
for key, item in listitem.items():
if key == "GROUP" or key == "TYPE":
temp[key] = item
print(temp)
myjson2.append(temp)
print(myjson2)
{'GROUP': 'A', 'TYPE': 'I'}
{'GROUP': 'B', 'TYPE': 'II'}
[{'GROUP': 'A', 'TYPE': 'I'}, {'GROUP': 'B', 'TYPE': 'II'}]
Try it online!
Solution 3:
It's because temp
keep reference to the same dictionary object and what you did in inside for key, item in listitem.items()
is essentially change the value of the dictionary and since the last value of the listitem
is {'GROUP': 'B', 'TYPE': 'II'}
, temp
will in the end has these keys-values. You can simply fix this by reference temp
to a new object in every new iteration
for listitem in myjson:
temp = {}
for key, item in listitem.items():
if key == "GROUP" or key == "TYPE":
temp[key] = item
print(temp)
myjson2.append(temp)