Last Key in Python Dictionary

后端 未结 10 845
栀梦
栀梦 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:21

    You can do a function like this:

    def getLastItem(dictionary):
        last_keyval = dictionary.popitem()
        dictionary.update({last_keyval[0]:last_keyval[1]})
        return {last_keyval[0]:last_keyval[1]}
    

    This not change the original dictionary! This happen because the popitem() function returns a tuple and we can utilize this for us favor!!

提交回复
热议问题