How to Get Last Key in Dictionary Python?

By Hardik Savani October 30, 2023 Category : Python

Hey Artisan,

This article will give you an example of python dictionary get last key. This post will give you a simple example of how to get last key in dictionary python. step by step explain how to get last key in python dictionary. Here you will learn python 3 get last key in dict. Follow the below tutorial step of python get last key in dictionary.

We will get last key from python dictionary using keys() and values() functions. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Example:

main.py

# Create New Dictionary Object
myDictionary = {
  "id": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}

# Get Last Key and Value from Dictionary
lastKey = list(myDictionary.keys())[-1]
lastValue = list(myDictionary.values())[-1]

print('Last Key: ', lastKey)
print('Last Value: ', lastValue)

Output:

Last Key:  email
Last Value:  hardik@gmail.com

I hope it can help you...

Tags :
Shares