From my db query in mongo, i get a <class 'pymongo.cursor.Cursor'> Object as lets say query_result where data is collection of dictionary such as
{
id:'1',
locations:
[
{lat:1.36, long:-21.99},
{lat:13.08, long:12.13},
{lat:33.08, long: 12.13}
]
}
{
id:'2',
locations:
[
{lat:1.42, long:-21.31},
{lat:2.36, long:11.12}
]
}
The lat long dict can be from 1 to many.
I need to convert the above dict into dictionary of list as
res = {
id: ['1','1','1','2','2'],
lat: [1.36, 13.08, 33.08, 1.42, 2.36],
long: [-21.99, 12.13, 12.13, -21.31, 11.12]
}
I tried using the following code:
res= dict(id=[], lat=[],long=[])
for result in query_result:`
for location in result['locations']:
res.update(id=res['id'] + result['id'],
lat=res['lat'] + location['lat'],
long=res['long'] + location['long'],
)
Only the ID field is in string while lat and long are Double.
The error i received was: TypeError: can only concatenate list (not "str") to list on line 4.