python
dictionary
keys
programming
tutorial

How to find the first key in a dictionary? python

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In modern Python, the first key in a dictionary is the first key inserted, because dictionaries preserve insertion order. The most direct way to get it is next(iter(d)), provided you also decide how to handle the empty-dictionary case.

Core Sections

Use next(iter(d))

The most efficient idiom is:

python
data = {"a": 1, "b": 2, "c": 3}
first_key = next(iter(data))
print(first_key)

iter(data) produces an iterator over keys, and next(...) retrieves the first one. This is efficient because it does not build an intermediate list of all keys.

Handle empty dictionaries safely

If the dictionary may be empty, next(iter(d)) raises StopIteration. You can provide a default value.

python
data = {}
first_key = next(iter(data), None)
print(first_key)

That makes the empty case explicit and avoids an exception when “no first key” is a valid result.

Why not use list(d.keys())[0]

This older pattern works, but it is less efficient because it creates a full list of keys just to read one element. It also makes the code look as if random indexing is a natural property of the dictionary key view, when what you really want is just the first iterator result.

When performance is not critical, the list conversion will still produce the expected answer for non-empty dictionaries. The point is not that it is always disastrously slow; the point is that it performs extra work and communicates the intent less directly than the iterator-based idiom.

python
first_key = list(data.keys())[0]

For small dictionaries the difference is minor, but next(iter(d)) is both cleaner and more efficient, so it is the preferred idiom.

Know the version assumption

The meaning of “first key” depends on dictionary ordering behavior. Since Python 3.7, insertion order is part of the language specification. In CPython 3.6 it was already true as an implementation detail, but earlier Python versions should not be relied on for this behavior.

So the normal modern assumption is:

  • Python 3.7 and later: first key means first inserted key
  • older Python: do not rely on dictionary order for semantics

If you need sorted order, say so explicitly

Sometimes people say “first key” when they really mean “smallest key” or “first key alphabetically.” That is a different operation.

Older code sometimes uses OrderedDict for this kind of reasoning. In modern Python, normal dictionaries already preserve insertion order, so you usually do not need OrderedDict just to obtain the first inserted key. Use it only when you specifically need its extra ordering-related methods and semantics.

python
data = {"z": 1, "a": 2, "m": 3}
first_sorted_key = min(data)
print(first_sorted_key)

Or, if you truly need sorted iteration:

python
first_sorted_key = sorted(data)[0]

That is not the same as insertion order, so the code should make the distinction clear.

Common Pitfalls

  • Using list(d.keys())[0] when next(iter(d)) is simpler and avoids building a full list.
  • Forgetting to handle the empty-dictionary case and then getting StopIteration unexpectedly.
  • Assuming “first key” means alphabetical order when modern Python dictionaries actually preserve insertion order.
  • Relying on dictionary order semantics in Python versions where that behavior was not guaranteed by the language.
  • Writing code that hides whether it wants insertion order or sorted order, making the intent unclear to readers.

Summary

  • In modern Python, the first dictionary key is the first inserted key.
  • The preferred idiom is next(iter(d)).
  • Use next(iter(d), default) if the dictionary might be empty.
  • Do not convert all keys to a list unless you actually need that list.
  • If you mean sorted order rather than insertion order, use min or sorted explicitly.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.