How to Get First Value in Dictionary Python?

By Hardik Savani October 30, 2023 Category : Python

Hi Friends,

In this tute, we will discuss python dictionary get first value. we will help you to give an example of how to get first value in dictionary python. you can see how to get first value in python dictionary. I explained simply about python 3 get first value in dict.

We will get first value 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": "Hardik Savani",
  "email": "hardik@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

I hope it can help you...

Tags :
Shares