How To Find the First Key In The Dictionary In Python – With Examples

The insertion order is maintained in the dictionary since version Python 3.7.

You can find the first key in the dictionary in Python using the list(yourdict.keys())[0] method.

This tutorial is only applicable only if you’re using Python version 3.7 or later.

If you try to get the first in the dictionary with python versions earlier than 3.7, you may get any random keys because of non-maintenance of the insertion order.

If You’re in Hurry…

You can get the first key in the dictionary using the following code.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4
}

first_key = list(yourdict.keys())[0]

print(first_key)

Output

    one

If You Want to Understand Details, Read on…

Different methods are available to find the first key in the dictionary in Python. Let us learn each method.

Checking Python Version Information

Python dictionaries are ordered since version 3.7.

You can check the python version using the below script.

Code

import sys

print(sys.version)

Output

3.8.2 (default, Sep  4 2020, 00:03:40) [MSC v.1916 32 bit (Intel)]

Using Keys() And List function

This section teaches you how to get the first key in the dictionary in Python using the dict.keys() method.

The keys() method returns a copy of the dictionary’s list of keys.

To find the first key of the dictionary,

  • Convert the dictionary keys into a list using the list() method.
  • Access the first item in the list using the index 0.

Use this method when you just want the first key of the dictionary.

Code

The following code demonstrates how to get the first key in the dictionary in Python.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4
}

first_key = list(yourdict.keys())[0]

print(first_key)

Output

    one

Using Items() and For Loop

This section teaches you how to get the first key in the dictionary in Python using the dict.items() method.

The items() method returns the key-value pair of the dictionary.

To find the first key of the dictionary using the items() method,

Use this method when you want the key and value of the first item in the dictionary.

Code

The following code demonstrates how to get the first key in the dictionary in Python.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4
}

for k,v in yourdict.items():
    print(k)
    break

Output

    one

Using Iters() method

This section teaches you how to use the iters() method to get the first key in the dictionary.

The iters() method returns an iterator object of the dictionary.

To get the first key of the dictionary,

  • get the iterator object of the dictionary using the iters() method
  • Move the cursor in the iterator to the first item using the next() method. You’ll have the first key in the dictionary.

Use it only when you need an iterator object of the dictionary and need to move forward or backwards during the iteration.

Code

The following code demonstrates how to use the iters() method to find the first key in the dictionary.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4
}

dict_iterator = iter(yourdict)

print(next(dict_iterator))

Output

    one

Get First N Keys In the Dictionary

This section teaches you how to find the first N keys in the dictionary.

To find the first N keys,

  • get the list of keys using the keys() method
  • Use the list slicing to get the first n keys.

For example, to get the first two items, use :2.

Code

The following code demonstrates how to get the first n keys of the dictionary.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4
}

n = 2

first_n_keys = list(yourdict.keys())[:n]

print(first_n_keys)

Output

    ['one', 'two']

Conclusion

You’ve learned how to find the first key in the dictionary in Python using various methods and how to find the first ’n’ keys in the dictionary.

If you have any questions, please feel free to comment below.

You May Also Like

Leave a Comment