I ended up using `dict[next(iter(dict))]`
I am getting the dict as one of two dicts nested in a list from a weather service API. I do this using a list comprehension to call the API with coordinates for each location in another list. A mouthful.
`resp = [fetch_api_data(location, request_id) for location in REQUEST_LOCATIONS]`
I then cycle through the list and, for each dict, persist a WeatherRequest instance to db like so:
`
for item in weather_data:
data = item[next(iter(item))]
WeatherRequest(
date=data['date'],
request_id=data['request_id'],
request_location=data['request_location'],
type_icon=data['type_icon'],
description=data['description'],
temperature=data['temperature'],
wind_speed=data['wind']['speed'],
wind_bearing=data['wind']['bearing'],
wind_gust=data['wind']['gust'],
rain_prob=data['rain_prob'],
latitude=data['latitude'],
longitude=data['longitude']
).save()
`
Thanks.
Kind regards,
- SD
- SD
On Mon, Jan 27, 2020 at 2:51 PM Bill Freeman <ke1g.nh@gmail.com> wrote:
Note that these give the only value. This won't work if you have more than one value in the dict, since you won't know which you will get. Where d is the dict:list(d.values())[0]orfor i in d.values():# use i hereprint(i)ord[list(d)[0]]I'm sure that there are other ways. There is certainly at least a way to play with the iterator protocol without using "for", but it may be harder to read. You could put break at the end of the loop above to make it more apparent that it only runs once.--On Mon, Jan 27, 2020 at 6:58 AM S D <zebra05@gmail.com> wrote:--I have a dictionary which contains one item ("current_location", which is a nested dict) and I would like to access that nested dict. However, I cannot use the key as the code will break if a different key is passed, e.g. "different_location".How can I access the first item in a dictionary without using a key? The dict looks like this:`{'current_location': {'date': '2020-01-27T10:28:24.148Z', 'type_icon': 'partly-cloudy-day', 'description': 'Mostly Cloudy', 'temperature': 68.28, 'wind': {'speed': 10.48, 'bearing': 178, 'gust': 12.47}, 'rain_prob': 0.02, 'latitude': '-33.927407', 'longitude': '18.415747', 'request_id': 31364, 'request_location': 'Current location'}}`Kind regards,
- SD
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB%2BAj0tFp_qOtqd3WMvPTfGkGy3q-jCFDwnrgheYjBk8NqP4bQ%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAH-SnCBkMAJqVetRU_X46r-pg-HLDZsuTt4iqeYN%2BSpjSmbttA%40mail.gmail.com.
No comments:
Post a Comment