Reputation: 9431
I have a JSON response from an API. The JSON response, 'results' is a list containing a number of individual results, called 'routes'.
For each result, within the results JSON response, I wish to add further information, such as what was included in the request that generated this response from the API.
An example of three shortened results ('routes'), within the overall results is shown here:
results =
[({u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK'}),
({u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK'}),
({u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK'})]
I wish to add their respective values. For the above example of 3 results, there are 3 origin coords, such as:
origincoords = ['51.41833327,0.115963078', '51.34666046,-0.210947524', '51.39574919,-0.045778021']
UPDATE - Thanks to Martineau, I have been able to insert the values correctly. However, I am now curious as to how I could insert into a different location. For example, to achieve:
The end result looking would look like -
({u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value},
u'origincoords': '51.39574919,-0.045778021'},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK'})]
Upvotes: 0
Views: 117
Reputation: 123473
What you said you want doesn't quite make sense:
({u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK'}
u'origincoords': '51.39574919,-0.045778021'})]
But this does:
({u'routes': [{u'bounds': {u'northeast': {u'lat': value,
u'lng': value},
u'southwest': {u'lat': value,
u'lng': value}},
u'copyrights': u'value',
u'overview_polyline': {u'points': u’value’},
u'summary': u’value’,
u'warnings': [],
u'waypoint_order': []}],
u'status': u'OK', # NOT the end of the dict
u'origincoords': '51.39574919,-0.045778021'})]
An easy way to get the latter, which uses izip
(or zip
), would be:
try:
from itertools import izip
except ImportError:
izip = zip # Python 3
origincoords = ['51.41833327,0.115963078',
'51.34666046,-0.210947524',
'51.39574919,-0.045778021']
for route, origincoord in izip(results, origincoords):
route['origincoord'] = origincoord
Update
If you instead wanted to insert the pairs into the 'bounds'
dictionary inside the first, [0]
, dictionary in the 'routes'
list of each of the dictionaries in the results
list, that could be done by using the following:
for route, origincoord in izip(results, origincoords):
route['routes'][0]['bounds']['origincoord'] = origincoord
Note that both lists and dictionaries use [
x
]
notation to access their contents. The x
must evaluate to an integer index for lists, or a hashable object (e.g. string, integer, tuple, etc) for dictionaries.
Upvotes: 1
Reputation: 1555
Here is one simple way of doing it without any zip
for index in range(len(results)):
results[index][u'origincoords'] = origincoords[index]
Upvotes: 1
Reputation: 11
The zip function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences. To add the coordinates in the result you may want to iterate over this list of tuples and assign it in for loop:
for result, coord in zip(results, origincoords):
result[u'origincoords'] = coord
Upvotes: 0