Working on a data-cleaning project in Python, I had to find the position (or index) of specific key-value pairs in a dictionary. At first, it seemed like a simple task, but I quickly realized that Python dictionaries don’t store items with built-in indexes like lists do.
After experimenting with different approaches, I discovered a few simple and reliable ways to get the dictionary index in Python.
In this tutorial, I’ll share the exact methods I use, with easy-to-follow examples and clear explanations.
You Might Need to Find a Dictionary Index in Python
When you’re working with large data sets or configurations, it’s common to store data as key-value pairs in a Python dictionary. However, sometimes you may need to find the position of a certain key or value, for example, when matching data, debugging, or exporting results in a specific order.
Python dictionaries are unordered in versions before 3.7, but from Python 3.7 onward, they maintain insertion order. That means we can now treat dictionary items as ordered sequences and find their index position if needed.
Method 1 – Use list() to Convert Dictionary Keys or Values
One of the simplest ways to find the index of an item in a Python dictionary is by converting its keys or values into a list. Once converted, we can use the index() method to find the position of a specific key or value.
Here’s how I usually do it:
states = {
"California": "CA",
"Texas": "TX",
"New York": "NY",
"Florida": "FL",
"Illinois": "IL"
}
# Convert dictionary keys to a list
keys_list = list(states.keys())
# Find the index of a specific key
key_to_find = "Florida"
index_position = keys_list.index(key_to_find)
print(f"The index of '{key_to_find}' is:", index_position)I executed the above example code and added the screenshot below.

In the above code, I first converted the dictionary keys into a list and then used the index() method to find the position of “Florida”. This approach is quick and works perfectly when you only need to find the index of a single key or value.
You can also do the same for dictionary values:
values_list = list(states.values())
value_to_find = "TX"
index_position = values_list.index(value_to_find)
print(f"The index of value '{value_to_find}' is:", index_position)This method is easy and works well for small to medium-sized dictionaries.
Method 2 – Use enumerate() to Find Index While Iterating
When I need both the key and its index while looping through a dictionary, I prefer using Python’s enumerate() function. It’s clean, efficient, and works great when you want to perform an action based on the index.
Here’s an example:
states = {
"California": "CA",
"Texas": "TX",
"New York": "NY",
"Florida": "FL",
"Illinois": "IL"
}
key_to_find = "New York"
for index, key in enumerate(states):
if key == key_to_find:
print(f"'{key_to_find}' found at index:", index)
breakI executed the above example code and added the screenshot below.

In this example, enumerate() gives me both the index and the key while iterating through the dictionary. This is especially useful when you need to perform operations based on the position of the item.
You can also use this same approach to find the index of a value instead of a key:
value_to_find = "IL"
for index, value in enumerate(states.values()):
if value == value_to_find:
print(f"Value '{value_to_find}' found at index:", index)
breakThis method is more flexible and doesn’t require converting the dictionary to a list first.
Method 3 – Use a Custom Function to Get Dictionary Index
If you frequently need to find dictionary indexes in Python, you can create a small reusable function. This makes your code cleaner and easier to maintain.
Here’s a function I often use:
def get_dict_index(dictionary, item, search_by='key'):
"""
Returns the index of a key or value in a dictionary.
search_by can be 'key' or 'value'.
"""
if search_by == 'key':
items_list = list(dictionary.keys())
elif search_by == 'value':
items_list = list(dictionary.values())
else:
raise ValueError("search_by must be 'key' or 'value'")
try:
return items_list.index(item)
except ValueError:
return -1 # Return -1 if item not found
# Example usage
states = {
"California": "CA",
"Texas": "TX",
"New York": "NY",
"Florida": "FL",
"Illinois": "IL"
}
key_index = get_dict_index(states, "Texas", "key")
value_index = get_dict_index(states, "NY", "value")
print("Index of key 'Texas':", key_index)
print("Index of value 'NY':", value_index)I executed the above example code and added the screenshot below.

This function gives you flexibility: you can search by key or value and easily reuse it in your Python projects. I find this approach especially useful in data analysis scripts where I often need to locate specific items.
Method 4 – Use next() and enumerate() Together
Another elegant Python trick is combining next() with enumerate() to get the index directly.
This method is concise and perfect when you only need the first match.
Here’s how it works:
states = {
"California": "CA",
"Texas": "TX",
"New York": "NY",
"Florida": "FL",
"Illinois": "IL"
}
key_to_find = "Texas"
index_position = next((i for i, key in enumerate(states) if key == key_to_find), -1)
print(f"The index of '{key_to_find}' is:", index_position)I executed the above example code and added the screenshot below.

This one-liner is one of my favorites because it’s both Pythonic and efficient. It returns -1 if the key isn’t found, making it safe to use in production code.
Method 5 – Find Index in a Dictionary of Lists or Nested Dictionaries
Sometimes, you might be working with more complex data structures, such as dictionaries containing lists or even nested dictionaries. In such cases, you can still use the above methods with small modifications.
Here’s an example of finding the index of a nested key:
employees = {
"E001": {"name": "John", "state": "California"},
"E002": {"name": "Emma", "state": "Texas"},
"E003": {"name": "Michael", "state": "Florida"},
"E004": {"name": "Olivia", "state": "New York"}
}
key_to_find = "E003"
keys_list = list(employees.keys())
index_position = keys_list.index(key_to_find)
print(f"The index of employee ID '{key_to_find}' is:", index_position)This method works great when you need to find the position of a specific dictionary key, even in nested data structures. You can extend this logic to work with values inside the nested dictionaries as well.
Bonus Tip – Get Both Key and Index as a List
If you want to see all keys with their indexes, Python makes that easy too. You can use a simple list comprehension to generate a list of tuples containing both key and index.
states = {
"California": "CA",
"Texas": "TX",
"New York": "NY",
"Florida": "FL",
"Illinois": "IL"
}
indexed_keys = [(index, key) for index, key in enumerate(states)]
print(indexed_keys)This gives you a list like [(0, ‘California’), (1, ‘Texas’), (2, ‘New York’), …]. It’s a handy way to visualize or debug dictionary order in Python.
While Python dictionaries don’t have built-in indexes like lists, there are several simple and efficient ways to find them. You can use list(), enumerate(), or even a custom function depending on your use case.
For quick lookups, converting keys or values to a list works perfectly. For more advanced scenarios or when working with large data sets, using enumerate() or a custom function gives you better control and readability.
You may also like to read:
- Check if a Python Set is Empty
- Add Item to Set in Python
- Create an Empty Set in Python
- Compare Lists, Tuples, Sets, and Dictionaries in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.