Get Last Key in Python Dictionary Tutorial Example

22-Feb-2023

.

Admin

Get Last Key in Python Dictionary Tutorial Example

Hi Dev,

I am going to explain you example of get last key in python dictionary tutorial example. I’m going to show you about python print last dictionary key. step by step explain python 3 get last key in dict. we will help you to give example of python get last key in dictionary. Alright, let’s dive into the steps.

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": "Piyush Kamani",

"email": "piyush@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: piyush@gmail.com

#Python