Reputation: 3345
I have a list sample_list
like this:
[
{
"Meta": {
"ID": "1234567",
"XXX": "XXX"
},
"bbb": {
"color":red"
},
{
"Meta": {
"ID": "78945612",
"XXX": "XXX"
},
"bbb": {
"color":"blue"
}
]
now I want to extract the field "ID" and the values then put them in a dict
object, I tried:
init_dict = {}
for item in sample_list:
init_dict['ID'] = item['Meta']['ID']
print(init_dict)
This returns:
{'ID': '1234567'}
{'ID': '78945612'}
Seems like the second value overwrite the first one, if I print from outside of the iteration:
init_dict = {}
for item in sample_list:
init_dict['ID'] = item['Meta']['ID']
print(init_dict)
This only return the last value:
{'ID': '78945612'}
Combining with the comment below, I realise the logic here is wrong, since the key name is always 'ID', I'VE updated the example in the question, can we take the value for ID
as the new key and taking the value for color
as the new value?
Expected output is something like
{'1234567':'red', '78945612':'blue'}
can someone help please? Thanks.
Upvotes: 0
Views: 60
Reputation: 1273
The problem is in the second line of a for loop. What you are doing is that you are creating a dictionary in this forloop. In any other language, you would get a syntax error, because you are trying to access un-initialised variable (in this case init_dict
) (in this case, if sample_list is empty, you will get an error that init_dict does not exist)
for item in sample_list:
init_dict = {} // here
init_dict['ID'] = item['Meta']['ID']
print(init_dict)
Simple fix is to move it outside of a for loop
init_dict = {}
for item in sample_list:
init_dict['ID'] = item['Meta']['ID']
print(init_dict)
color edit:
init_dict = {}
for item in sample_list:
init_dict[item['Meta']['ID']] = item['bbb']['color']
print(init_dict)
Here you can see that you can use anything as a key, but be careful, the key has to unique, so in this case, ID is a very good key, but if there is a chance that you can get a duplicate ID, I recommend you to find a better key. Also, if you do not need the dictionary in first place, I recommend using just a list of tuples (i.e -> [(key, val), (key2, val2), (key3, val3)]
. The list has no problem with duplicities
Upvotes: 2
Reputation: 125
You need to append to your dictionary. The line init_dict = {}
resets your dictionary. Instead you should do:
init_dict = {'ID':[]}
for item in sample_list:
init_dict['ID'].append(item['Meta']['ID'])
print(init_dict)
To satisfy the request you made in a comment, you can do this to use the value as a key and then add a colour:
init_dict = {}
for item in sample_list:
init_dict[item['Meta']['ID']] = "red"
Upvotes: 0
Reputation: 830
Every time you are looping, you are just overwriting the same dictionary. Now, it prints both the elements in the first method because your print statement is inside the for loop. In the second method, your print statement is outside the for loop. So, it prints only the last value that you gave which is 78945612
.
Here you are just overwriting the same dictionary. So, every time you change the ID
, it just stores the last value that you gave.
Upvotes: 0