Python Iterate through Dictionary by Index

Recently, I was working on a project where I had to process customer data stored in a Python dictionary. The challenge was that I needed to access dictionary items not just by key or value, but by their index position.

At first, this felt tricky because dictionaries in Python are not traditionally indexed like lists. But with a few simple techniques, I found multiple ways to iterate through a dictionary by index.

In this tutorial, I’ll share the exact methods I use. I’ll cover different approaches, explain how each works, and provide full code examples. By the end, you’ll know exactly how to handle dictionary iteration by index in Python.

Methods to Iterate Through a Dictionary by Index

While dictionaries are designed for key-value access, there are real-world scenarios where index-based iteration makes sense.

For example:

  • You might want to display the first five items of a dictionary.
  • You may need to process items in a specific order, such as customer IDs sorted alphabetically.
  • You could be debugging and want to see which item appears at a certain index.

Since Python 3.7+, dictionaries maintain insertion order. This means the order in which you add items is preserved, making index-based iteration much more predictable.

Method 1 – Use enumerate() with items()

The simple way I use is to combine enumerate() with the items() methods in Python. enumerate() gives both the index and the key-value pair.

Example

# Sample dictionary with US state populations
state_population = {
    "California": 39538223,
    "Texas": 29145505,
    "Florida": 21538187,
    "New York": 20201249,
    "Pennsylvania": 13002700
}

# Iterate with index using enumerate
for index, (state, population) in enumerate(state_population.items()):
    print(f"Index: {index}, State: {state}, Population: {population}")

Output

Index: 0, State: California, Population: 39538223
Index: 1, State: Texas, Population: 29145505
Index: 2, State: Florida, Population: 21538187
Index: 3, State: New York, Population: 20201249
Index: 4, State: Pennsylvania, Population: 13002700

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

python iterate dictionary

This method is my go-to because it’s clean, readable, and works in almost every case.

Method 2 – Convert Keys to a List

Another way is to convert dictionary keys to a list and then access items by index in Python. This is useful when you need random access, not just iteration.

Example

# Convert keys to list
keys_list = list(state_population.keys())

# Access dictionary by index
for i in range(len(keys_list)):
    key = keys_list[i]
    value = state_population[key]
    print(f"Index: {i}, State: {key}, Population: {value}")

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

iterate through dictionary python

This approach feels familiar if you’re used to working with lists. However, it requires extra memory to store the list of keys.

Method 3 – Use zip() with Range

Sometimes, I prefer to combine zip() with the range() method in Python for more control.

Example

# Iterate using zip with range
for i, key in zip(range(len(state_population)), state_population):
    print(f"Index: {i}, State: {key}, Population: {state_population[key]}")

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

python iterate over dictionary

This works well when I want to explicitly manage both the index and the dictionary key.

Method 4 – Iterate Over Sorted Keys

In many real-world scenarios, I need results sorted alphabetically or numerically. Python makes this easy by sorting the dictionary keys first.

Example

# Iterate over sorted dictionary keys
for i, key in enumerate(sorted(state_population.keys())):
    print(f"Index: {i}, State: {key}, Population: {state_population[key]}")

This is especially useful when presenting data to users or exporting it in a structured format.

Method 5 – Use itertools.islice() for Slicing

If I only want a specific range of dictionary items, I use Python’s itertools.islice().

Example

from itertools import islice

# Get first 3 items from dictionary
for i, (state, population) in enumerate(islice(state_population.items(), 3)):
    print(f"Index: {i}, State: {state}, Population: {population}")

This method is efficient because it doesn’t require converting the entire dictionary to a list.

Performance Considerations

While all methods work, there are performance differences.

  • enumerate() with items() is the most Pythonic and memory-efficient.
  • Converting keys to a list is slightly slower and uses more memory.
  • Sorting keys adds overhead, but is useful when order matters.
  • islice() is best when you only need a portion of the Python dictionary.

For large datasets, I recommend sticking with enumerate() or islice().

Real-World Example: Process Customer Orders

Let’s take a real-world example. Suppose I’m working with an e-commerce dataset where each order ID maps to the order amount.

I want to display the first five orders to verify the data.

Example

orders = {
    "ORD1001": 250.75,
    "ORD1002": 99.99,
    "ORD1003": 145.50,
    "ORD1004": 320.00,
    "ORD1005": 75.25,
    "ORD1006": 210.40
}

# Display first 5 orders
for i, (order_id, amount) in enumerate(orders.items()):
    if i < 5:
        print(f"Index: {i}, Order ID: {order_id}, Amount: ${amount}")

This approach helped me quickly validate the order data without manually searching through the dictionary.

  • Dictionaries in Python preserve insertion order (Python 3.7+).
  • You can iterate by index using enumerate(), list conversion, zip(), sorted keys, or islice().
  • Choose the method based on your use case: readability, performance, or order requirements.

Most of the time, I find myself using enumerate() with items() because it’s simple and efficient. But knowing the alternatives gives me flexibility when working on larger projects.

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.