Get First Key in Python Dictionary Tutorial Example

21-Feb-2023

.

Admin

Get First Key in Python Dictionary Tutorial Example

Hi Dev,

Now, let's see tutorial of get first key in python dictionary tutorial example. you can understand a concept of python print first dictionary key. step by step explain python get first key in dictionary. you can understand a concept of python 3 get first key in dict. follow bellow step for how to get first key in python dictionary.

We will get first key from python dictionary using next() and iter() 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 First Item from Dictionary

firstPair = next(iter((myDictionary.items())) )

print('First Key Value Pair of Dictionary is:')

print(firstPair)

print('First Key: ', firstPair[0])

print('First Value: ', firstPair[1])

Output:

First Key Value Pair of Dictionary is:

('id', 1)

First Key: id

First Value: 1

#Python