Last Key in Python Dictionary

后端 未结 10 2151
轻奢々
轻奢々 2021-01-31 13:32

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:26

    sorted(dict.keys())[-1]
    

    Otherwise, the keys is just an unordered list, and the "last one" is meaningless, and even can be different on various python versions.

    Maybe you want to look into OrderedDict.

提交回复
热议问题