Python
Dictionary
Data Structures
Programming
Python Tips

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:

python
1d = {"name": "Alice", "age": 30, "city": "Toronto"}
2
3first_key = next(iter(d))
4print(first_key)

First value:

python
first_value = next(iter(d.values()))
print(first_value)

First key-value pair:

python
first_item = next(iter(d.items()))
print(first_item)

Output:

text
name
Alice
('name', 'Alice')

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.

python
1d = {}
2
3first_key = next(iter(d), None)
4first_item = next(iter(d.items()), None)
5
6print(first_key)
7print(first_item)

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:

python
first_key = list(d)[0]
first_item = list(d.items())[0]

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.

python
1from itertools import islice
2
3d = {"a": 1, "b": 2, "c": 3, "d": 4}
4
5first_two = list(islice(d.items(), 2))
6print(first_two)

Output:

text
[('a', 1), ('b', 2)]

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.

Course illustration
Course illustration

All Rights Reserved.