I want to store the values in an existing dictionary into another dictionary while traversing through all the items? How can this be done? I have tried to access all items using this:
res = {
"responseCode": 0,
"responseDesc": [
{
"city_id": 1,
"city_name": "Mumbai",
"description": "Mumbai",
"latitude": 18.987807,
"longitude": 72.836447,
"total_trips": 0
}
]
}
temp = {}
for responseDesc in res :
temp = (res[responseDesc])
The above statement in for loop throws an error. If I try to print the values instead of storing them in the temp dictionary it shows them correctly. How can I achieve this? Thanks in advance.
try below code
temp = {}
for responseDesc in res :
if responseDesc =='responseDesc':
temp = res[responseDesc]
If you are just looking to take a copy in the easiest possible way, use temp=dict(res)
If you are looking at iterating through all the nested layers of the json you can:
If you want to iterate through the responseDesc array: for responseDesc in res['responseDesc']:
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.