How to get first key in Dictionary – Python

In this article we will discuss different ways to fetch the first key of a dictionary. Then we will look how to select first N keys of a dictionary in python.

Table of contents

Get first key from a dictionary using keys() method

In python, dictionary is a kind of container that stores the items in key-value pairs. It also provides a function keys(), that returns an iterable sequence of all keys in the dictionary. We will use that to fetch all keys in the dictionary and then we will select first key from that sequence i.e.

# Dictionary of string and int
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

# Get the first key in a dictionary
first_key = list(word_freq.keys())[0]

print('First Key of dictionary:')
print(first_key)

Output:

First Key of dictionary:
Hello

Here we converted the iterable sequence of keys to a list and then selected the first element from the list i.e. first key of the dictionary

Advertisements

Method 2:

If we try to convert dictionary to list by passing the dictionary object to list constructor list(), then implicitly it will convert all keys of the dictionary to list and then we can select the first element from the list i.e. first key of the dictionary,

# Dictionary of string and int
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

# Get the first ket in a dictionary
first_key = list(word_freq)[0]

print('First Key of dictionary:')
print(first_key)

Output:

First Key of dictionary:
Hello

Here we didn’t even called the keys() function. We just created a list of keys from the dictionary and selected first item from it.

Related Stuff:

Get first key in a dictionary using iter() & next()

In in the above solution, we created a list of all the keys and then selected the first key. It was not an efficient solution, because if our dictionary is large and we need only the first key, then why are we creating a huge list of all keys. As keys() returns an iterable sequence of dictionary keys, so we can create an iterator object of this iterable sequence of keys using iter() function. Then by calling the next() function on iterator object, we can get the first element of this sequence i.e. first key of the dictionary. For example,

# Dictionary of string and int
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

# Get the first ket in a dictionary
first_key = next(iter(word_freq))

print('First Key of dictionary:')
print(first_key)

Output:

First Key of dictionary:
Hello

This is an efficient solution because didn’t iterated over all the keys in dictionary, we just selected the first one.

Get first N keys from a Dictionary in Python

To select first N keys from a dictionary, convert the keys of a dictionary to a list and then select first N entries from that. For example, let’s see how to select first 3 keys from a dictionary,

# Dictionary of string and int
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

# get list of first 3 keys from the dictionary
n = 3
first_n_keys = list(word_freq.keys())[:n]

print(f'First {n} keys in the dictionary:')
print(first_key)

Output:

First 3 keys in the dictionary:
Hello

Conclusion:

We learned about different about different ways to select first key from a dictionary in python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top