Skip to content

How to Get the First Key in a Dictionary in Python

Learn the most efficient ways to retrieve the first inserted key from a Python dictionary (Python 3.7+) using next(iter()) or list conversion, with compatibility notes for older versions.

Unlike lists, which are accessed by an integer index (e.g., my_list[0]), dictionaries are accessed by their keys. However, a common task is to retrieve the first key that was inserted into a dictionary. Since Python 3.7, standard dictionaries are guaranteed to preserve the order in which items were added, making this a reliable operation.

This guide will demonstrate the two most Pythonic ways to get the first key of a dictionary, explaining the best use case for each method and providing a critical note for those working with older versions of Python.

Understanding Dictionary Ordering (Python 3.7+)

Before Python 3.7, standard dictionaries were considered unordered. This meant that the sequence of items could change, and you could not reliably get the "first" key.

Method 1: Using next() and iter() (Most Efficient)

If you only need the very first key and nothing else, this is the most memory-efficient method. It avoids creating a new list in memory.

  • iter(my_dict): Creates an iterator object over the dictionary's keys.
  • next(...): Retrieves the next (in this case, the first) item from the iterator.

Solution:

my_dict = {"first_key": 1, "second_key": 2, "third_key": 3}

first_key = next(iter(my_dict))

print(first_key)
print(f"The value of the first key is: {my_dict[first_key]}")

Output:

first_key
The value of the first key is: 1

Method 2: Converting Keys to a List (More Flexible)

If you need to access keys by their numerical position (e.g., the first, second, or last key), you should first convert the dictionary's keys into a list.

Solution:

my_dict = {"first_key": 1, "second_key": 2, "third_key": 3}

list_of_keys = list(my_dict)

first_key = list_of_keys[0]
last_key = list_of_keys[-1]

print(f"The first key is: '{first_key}'")
print(f"The last key is: '{last_key}'")

Output:

The first key is: 'first_key'
The last key is: 'third_key'

Important Consideration: Python Versions Before 3.7

Solution for Older Python:

import collections

my_ordered_dict = collections.OrderedDict([
    ("first_key", 1),
    ("second_key", 2),
    ("third_key", 3)
])

first_key = next(iter(my_ordered_dict))

print(first_key)

Output:

first_key

Conclusion

If you need... The best solution is... Why?
Only the first key. next(iter(my_dict)) Most memory-efficient, as it doesn't create a new list.
The first, last, or any key by its position. list(my_dict)[index] More flexible, but uses more memory by creating a list of keys.
Guaranteed order in Python < 3.7. collections.OrderedDict Standard dict was unordered in older Python versions.

For modern Python (3.7+), both next(iter(my_dict)) and list(my_dict)[0] are reliable ways to get the first key of a dictionary, with the former being more efficient for that specific task.

Navigation