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]

or

    for i in d.values():
        # use i here
        print(i)

or

    d[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 <zebr...@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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAH-SnCBnnOsoTURnSzCrqqXsCWF5EEjghm9Q1UTQKrnTBAJ3iA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@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.

Reply via email to