Get First Key Value Pair in Python Dictionary

20-Feb-2023

.

Admin

Get First Key Value Pair in Python Dictionary

Hi Dev,

This tutorial is focused on get first key value pair in python dictionary. we will help you to give example of python dictionary first key value. you will learn how to get first key value pair in dictionary python. I would like to show you how to get first key value in dictionary python.

We will get first key value pair 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