How To Get First Key From Dictionary Python – 3 Methods

In this tutorial, you will learn how to get first key from dictionary Python using 4 Methods.

There were various methods or you can call a function that you can use to retrieve the first key from a dictionary in Python. For example, you can use the keys() function that list all the keys and the key at the first index is the first key in the dictionary.

Note that you need to make sure that you are using an Ordered Dictionary in Python.

Let us see all the different methods that can be used to get the first key from a dictionary in Python.

1. Using The Keys() Function To Get First Key From Dictionary

If you call keys() on a dictionary, it returns a list of the keys contained in the dictionary. You can retrieve the first key from this list by using the list’s first index element.

Let us see in the below example code the usage of the keys() function on the dictionary in Python.

#Initializing the Dictionary
newDict = {1:"You", 2: "Are", 3:"On", 4:"Coduber"}

#Using Keys Method to get the First Key
firstKey = list(newDict.keys())[0]

#Print First Key of Dictionary
print(firstKey)

Output:

The First Key from the Dictionary is 1.

2. Using the Iter Function To Get First Key From Dictionary

In Python, Iter() function returns the first key it retrieves from the dictionary. If you are using the ordered dictionary then the return element will be the first key but if you are using the unordered dictionary then you can get any random key from the dictionary that needs not be the first one.

Let us see in the below code example how to get the first key from a dictionary Python.

#Initializing the Dictionary
newDict = {1:"You", 2: "Are", 3:"On", 4:"Coduber"}

#Using iter Method to get the First Key
firstKey = next(iter(newDict))

#Print First Key of Dictionary
print("The First Key from the Dictionary is {}.".format(firstKey))

Output:

The First Key from the Dictionary is 1.

3. Using the items() Function To Get First Key From Dictionary

In Python, if you are calling items function on the dictionary it will return a list of all the keys and value pairs. Using this key-value pair you can get the first key from the list using the index method.

Let us see in the below example code how you can get the key using the items function in Python.

#Initializing the Dictionary
newDict = {1:"You", 2: "Are", 3:"On", 4:"Coduber"}

#Using Keys Method to get the First Key
firstKey = list(newDict.items())[0][0]

#Print First Key of Dictionary
print("The First Key from the Dictionary is {}.".format(firstKey))

Output:

The First Key from the Dictionary is 1.
How To Get First Key From Dictionary Python - 3 Methods

Wrap Up

I hope you understood how to get the first key from the dictionary Python. But there is a catch to this problem you can only get the first key from any dictionary in Python when you are using an Ordered dictionary. Otherwise, there is exactly no method to get the first Key from the unordered dictionary in Python.

Let me know in the comment section you know any better method than the one discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Check If A Number Is Even In Python
  2. How To Change A Variable Outside Of A Function In Python
  3. How To Compare String and Integer in Python

Leave a Comment