Last Key in Python Dictionary

后端 未结 10 867
栀梦
栀梦 2021-01-31 13:58

I am having difficulty figuring out what the syntax would be for the last key in a Python dictionary. I know that for a Python list, one may say this to denote the last:

10条回答
  •  心在旅途
    2021-01-31 14:00

    Since python 3.7 dict always ordered(insert order),

    since python 3.8 keys(), values() and items() of dict returns: view that can be reversed:

    to get last key:

    next(reversed(my_dict.keys()))  
    

    the same apply for values() and items()

    PS, to get first key use: next(iter(my_dict.keys()))

提交回复
热议问题