简体   繁体   中英

List generated from For loop with .append() of another JSON data

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'}]

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!

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]

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM