HOWTO · Python
How to Get the First Key in Python Dictionary
Learn to get the first key yielded by a Python dictionary iterator with next(), list conversion, a for loop, and a safe default for empty dictionaries.
On this page
To get the first key yielded by a Python dictionary, call next(iter(my_dict)). This reads one key directly from the dictionary iterator. In this example, the dictionary contains a name and city, and the program prints the key yielded first:
my_dict = {"name": "Ada", "city": "London"}
first_key = next(iter(my_dict))
print(first_key)
What “First” Means in a Dictionary
A dictionary does not provide numeric indexing for keys. Here, “first” means the first key yielded when Python iterates over the dictionary. For an ordered dictionary, iteration follows its insertion order, so the first key is the earliest inserted key. If a mapping uses a different iteration order, “first” follows that mapping’s iteration behavior. This definition describes the iteration result; it does not assume that every mapping orders its keys the same way.
Dictionary iteration yields keys by default. You can therefore pass the dictionary directly to iter() or a for loop. You do not need to call keys() first. The my_dict.keys() method returns a dynamic view of the keys when you need to inspect or reuse that view separately.
Get the First Key with next()
Use iter() to create an iterator over the dictionary, then next() to retrieve its next item. Since a dictionary iterator yields keys, this approach reads one key without building a separate list of all keys. For a nonempty dictionary, it is the concise choice when you only need one key:
my_dict = {"name": "Ada", "city": "London"}
first_key = next(iter(my_dict))
print(first_key)
The value of first_key is the first key produced by iteration. next() also accepts an optional default, which is useful when the dictionary may have no keys. The final section shows that form.
Get the First Key with list()
If you also need the keys as a list, convert the dictionary to a list and select index 0. A dictionary passed to list() yields its keys, so there is no need to call keys() first:
my_dict = {"name": "Ada", "city": "London"}
first_key = list(my_dict)[0]
print(first_key)
This conversion materializes every key before selecting one. Use the iterator approach when you only need a single key. List conversion is useful when the resulting list will also be indexed, sliced, or used in another list operation. Index 0 requires the list to contain a key, so this form is not suitable when the dictionary may be empty unless you check it first.
Get the First Key with a for Loop
A for loop iterates over dictionary keys directly. Assign the first key, then use break to stop after that iteration:
my_dict = {"name": "Ada", "city": "London"}
for key in my_dict:
first_key = key
break
print(first_key)
This approach fits code that already uses a loop or needs to perform an operation with the key before stopping. It avoids converting all keys into a list. The loop does not need items() when the value is unused. If the dictionary contains no keys, the loop body never runs, so code that uses first_key afterward should account for that possibility.
Handle an Empty Dictionary
Calling next(iter(my_dict)) when the dictionary has no keys raises StopIteration. Pass a second argument to next() to return a fallback instead:
my_dict = {}
first_key = next(iter(my_dict), None)
print(first_key)
Here, None is the fallback. Choose a default that cannot be confused with a valid dictionary key. If None can be a key in your data, create a unique sentinel object and use it as the default:
my_dict = {"name": "Ada", "city": "London"}
sentinel = object()
first_key = next(iter(my_dict), sentinel)
if first_key is sentinel:
print("No keys")
else:
print(first_key)
The list approach with list(my_dict)[0] has no default argument and raises IndexError when there are no keys. Use next(iter(my_dict), default) when an inline fallback makes the empty-input behavior easier to handle.