Get the First Key in a Python Dictionary

Recently, I was working on a project where I had to quickly retrieve the first key from a Python dictionary. At first, it sounded simple, but when I dug deeper, I realized that dictionaries don’t behave exactly like Python lists.

The challenge was that dictionaries are unordered collections in older versions of Python. But starting from Python 3.7, dictionaries maintain insertion order. This means the first key is always the first one inserted.

In this tutorial, I’ll share the different methods I personally use to get the first key in a Python dictionary. I’ve been coding in Python for more than 10 years, and I’ll show you both beginner-friendly and advanced approaches.

Method 1: Use Python’s list(dict.keys())[0]

The first method I often use is converting the dictionary keys into a list and then accessing the first element. This works because dict.keys() returns a view object, which can easily be converted into a list.

# Example: Get first key in Python dictionary using list()
my_dict = {
    "California": "Los Angeles",
    "Texas": "Houston",
    "Florida": "Miami",
    "New York": "Buffalo"
}

first_key = list(my_dict.keys())[0]
print("The first key is:", first_key)

When I run this code, the output is:

The first key is: California

You can refer to the screenshot below to see the output.

python get first element of dict

This method is easy, but it creates an extra list in memory, which may not be efficient for very large dictionaries.

Method 2: Use next(iter(dict))

Another clean and Pythonic way is to use an iterator. The iter() function creates an iterator over the dictionary keys, and next() retrieves the first element.

# Example: Get first key using next() and iter()
my_dict = {
    "California": "Sacramento",
    "Texas": "Austin",
    "Florida": "Tallahassee",
    "New York": "Albany"
}

first_key = next(iter(my_dict))
print("The first key is:", first_key)

The output is:

The first key is: California

You can refer to the screenshot below to see the output.

get first element of dictionary python

I prefer this method when I want a memory-efficient solution, especially for large dictionaries.

Method 3: Use for Loop in Python

Sometimes, I just want to loop through the dictionary and grab the first key without creating extra objects.

A simple for loop can do the trick.

# Example: Get first key using a loop
my_dict = {
    "California": "Golden State",
    "Texas": "Lone Star State",
    "Florida": "Sunshine State",
    "New York": "Empire State"
}

for key in my_dict:
    first_key = key
    break

print("The first key is:", first_key)

The output is:

The first key is: California

You can refer to the screenshot below to see the output.

python get first element of dictionary

This method is very readable and works in all versions of Python.

Method 4: Use Python’s collections.OrderedDict

Before Python 3.7, dictionaries didn’t guarantee insertion order. If you’re working with older versions, you can use OrderedDict from the collections module.

from collections import OrderedDict

# Example: Get first key using OrderedDict
my_dict = OrderedDict([
    ("California", "West Coast"),
    ("Texas", "South"),
    ("Florida", "Southeast"),
    ("New York", "Northeast")
])

first_key = next(iter(my_dict))
print("The first key is:", first_key)

The output is:

The first key is: California

You can refer to the screenshot below to see the output.

how to get first element of dictionary python

I used this approach often when I was maintaining legacy Python 3.5 projects.

Method 5: Use Dictionary Unpacking

In Python 3.6+, you can even use unpacking tricks to grab the first key.

# Example: Get first key using unpacking
my_dict = {
    "California": "West Coast",
    "Texas": "South",
    "Florida": "Southeast",
    "New York": "Northeast"
}

first_key, *_ = my_dict
print("The first key is:", first_key)

The output is:

The first key is: California

This method is less common but elegant if you like Python’s unpacking syntax.

Practical Example: Find First Key in Real Data

Let’s say I have a dataset of U.S. states and their populations. I want to quickly check the first state in the dictionary.

# Real-world example: Get first state in dictionary
us_population = {
    "California": 39538223,
    "Texas": 29145505,
    "Florida": 21538187,
    "New York": 20201249
}

first_state = next(iter(us_population))
print("The first state is:", first_state)
print("Population:", us_population[first_state])

Output:

The first state is: California
Population: 39538223

This is a practical way I use the method when working with U.S. datasets.

Which Method Should You Use?

  • If you want simplicity, go with list(dict.keys())[0].
  • If you want efficiency, use next(iter(dict)).
  • If you’re working with older Python versions, use OrderedDict.
  • If you like readable code, use a for loop.
  • If you want to try something Pythonic, use unpacking.

While Python dictionaries don’t have a built-in way to directly fetch the first key, there are multiple approaches you can use.

Personally, I rely on next(iter(dict)) most of the time because it’s fast and memory-friendly. But if I’m teaching beginners, I often show the list(dict.keys())[0] method first since it’s easier to understand.

So the next time you’re working with a Python dictionary and need the first key, you’ll know exactly which method to use.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.