I think the real question here is why you have a dict with one item in it?
If you can construct it so that this isn't the case that would be ideal.
If you really can't construct it any other way then you can do something simple like my_dict.popitem()[1] to get the value of the only item in the dict. Although it is worth noting this will also remove that item from the dict (giving you a single non-nested dict which seems more desirable).
- Nick
On Mon, Jan 27, 2020 at 12:32 PM Mike Dewhirst <miked@dewhirst.com.au> wrote:
--Can you use ddict[ddict.keys()[0]] ???I don't know if that would show you the first item unless it was an ordered dict.Mike-------- Original message --------From: S D <zebra05@gmail.com>Date: 27/1/20 22:56 (GMT+10:00)Subject: Getting the first item in a dict--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/5e2ed855.1c69fb81.32672.ad44SMTPIN_ADDED_MISSING%40gmr-mx.google.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/CAGuvt90w7nW4cGBRjX%2BDtA5%2BvHk9i17rnfwjmF8o1XOZJ_QJyA%40mail.gmail.com.
No comments:
Post a Comment