retrieve key of only element in a dictionary (Python 3)

Chris Angelico rosuav at gmail.com
Sat Mar 19 04:42:29 EDT 2016


On Sat, Mar 19, 2016 at 7:29 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote:
>
>> So unpacking will give you those keys - in an arbitrary order. Of
>> course, you don't care about the order when there's only one.
>
> But what if you want the key in reverse order?
>
> # Standard order
> [key] = mydict
>
> # Reverse order
> [yɘʞ] = mydict

Unfortunately, Steven, that won't work - you forgot to invert the
brackets. And no, switching them won't work:

]yɘʞ[ = mydict

You need to have actual backward brackets so Python knows you want to
iterate backward through the dictionary, starting with the last value
and proceeding to the first index.

ChrisA



More information about the Python-list mailing list