Reputation: 89
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'}]
Upvotes: 2
Views: 969
Reputation: 38502
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]
Upvotes: 1
Reputation: 4480
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)
Upvotes: 1
Reputation: 43934
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'}]
Upvotes: 1