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.
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:
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.
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.
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.
Or, if you truly need sorted iteration:
That is not the same as insertion order, so the code should make the distinction clear.
Common Pitfalls
- Using
list(d.keys())[0]whennext(iter(d))is simpler and avoids building a full list. - Forgetting to handle the empty-dictionary case and then getting
StopIterationunexpectedly. - 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
minorsortedexplicitly.
Related reading
- how to find the height of a node in binary tree recursively
- How to find the index of an element in a TreeSet?
- How to find the kth largest element in an unsorted array of length n in On?
- How to find the kth smallest element in the union of two sorted arrays?
- How to find the installed pandas version
- How to find the mime type of a file in python?
- How to find the Largest Difference in an Array
- How to find the length of a linked list that is having cycles in it?

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 courseTrack 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.