Skip to main content

How to Get the First Key in a Dictionary in Python

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.

note

Since Python 3.7, dictionaries are officially guaranteed to maintain insertion order. This means the items are stored in the same sequence in which you added them, and iterating over the dictionary will always follow that order. This change makes getting the first key a predictable and useful operation.

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}

# Get an iterator for the dictionary's keys and retrieve the first one.
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(my_dict) is a shortcut to get a list of the dictionary's keys.
list_of_keys = list(my_dict)

# Now you can access any key by its integer index.
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'
note

While this method is more flexible, it is less memory-efficient than next(iter(my_dict)) because it creates an entirely new list of all the keys in memory.

Important Consideration: Python Versions Before 3.7

warning

Legacy Python (versions 3.6 and older) If you are working with a Python version before 3.7, you cannot rely on the standard dict to preserve insertion order. For these versions, you must use collections.OrderedDict.

Solution for Older Python:

import collections

# Use OrderedDict to guarantee insertion order in Python < 3.7
my_ordered_dict = collections.OrderedDict([
("first_key", 1),
("second_key", 2),
("third_key", 3)
])

# Now you can safely get the first key
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.OrderedDictStandard 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.