Get first element from a dictionary
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, getting the “first” element of a dictionary means reading the first key, value, or key-value pair in insertion order. Since modern Python preserves insertion order for dictionaries, this is usually well-defined, but the right way to access it is through an iterator rather than numeric indexing.
What “First” Means for a Dictionary
A dictionary is not a list, so d[0] does not mean “give me the first item.” It means “look up the value stored under key 0.” If no such key exists, Python raises KeyError.
The correct mental model is:
- dictionaries are keyed collections
- iteration follows insertion order in Python 3.7 and later
- the first element is the first inserted item still present in the dictionary
So when people ask for the first element, they usually want one of these:
- the first key
- the first value
- the first key-value pair
The Most Efficient Pattern
The cleanest approach is next(iter(...)). It does not build an intermediate list, so it is both fast and memory-efficient.
First key:
First value:
First key-value pair:
Output:
These forms are preferred over list(d)[0] or list(d.items())[0] because they avoid copying the entire dictionary view into a list just to read one element.
Handling Empty Dictionaries Safely
The only catch is that next(iter(d)) raises StopIteration when the dictionary is empty. If that is possible in your code, pass a default.
This makes the empty case explicit and avoids exceptions for normal control flow.
When a List Conversion Is Acceptable
You will still see code like this:
It works, but it builds a full list first. For small dictionaries in throwaway scripts, that may be fine. For larger dictionaries or hot code paths, it is unnecessary overhead.
A good rule is simple: if you only need one element, iterate once; do not materialize the whole collection.
Getting More Than One Leading Item
If you want the first few entries rather than just one, itertools.islice is a good fit.
Output:
This keeps the iterator model while giving you a controlled number of leading items.
Version and Ordering Notes
In Python 3.7 and later, insertion order for dictionaries is part of the language specification. In CPython 3.6, it was already true in practice, but it was not yet a formal language guarantee. In much older Python versions, dictionary order should not be treated as stable.
If you are maintaining very old code and truly need guaranteed order, collections.OrderedDict may still be relevant. In modern Python, a normal dict is usually enough.
Common Pitfalls
The most common mistake is writing d[0] and expecting positional access. Dictionaries do not work that way unless 0 is literally a key.
Another mistake is using list(d)[0] everywhere. It is readable, but it wastes work when a single next(iter(d)) would do the job without building a list.
It is also easy to forget the empty-dictionary case. next(iter(d), None) is a useful default pattern when an empty input is possible.
Summary
- In modern Python, the first dictionary element is the first inserted item still present.
- Use
next(iter(d))for the first key. - Use
next(iter(d.values()))for the first value. - Use
next(iter(d.items()))for the first key-value pair. - Pass a default to
next()when the dictionary may be empty.

