How to Iterate through a Dictionary in Python with Index? [4 Methods]

Python provides various ways to iterate through a Python dictionary with an index for accessing keys and values.

A dictionary in Python is used to store data in the form of a key-value pair. As of the Python 3.7 version, Python dictionaries are ordered so that we can access the dictionary keys by index.

In this Python article, I will explain “How to iterate through a dictionary in Python with index” using four different methods. I will also explain each of the methods with the help of some examples.

We use loops to iterate over sequences or series in Python, so we can also use the for loop to iterate through a dict with an index in Python.

Iterate through a Dictionary in Python with Index

Here is the list of all the methods that can be used in Python to iterate dictionary by index:

  1. Using for loop
  2. Enumerate() Function
  3. Using items() Method
  4. Using sorted() Function

Let’s see all of them one by one using some examples:

1. Iterate Over Python Dictionaries Using For Loops

The most basic way to iterate through a dict in Python is by for loop. The loop is used to iterate over the keys of the dictionary, and we can access the respective values and index.

Here is an example of how to iterate over a dictionary in Python with key using a for loop:

USA_states = {
    "New York": "Albany",
    "California": "Sacramento",
    "Ohio": "Columbus",
    "Texas": "Austin",
    "Washington": "Olympia"
}

index = 0
for key in USA_states:
    value = USA_states[key]
    print(f"Index: {index}, Key: {key}, Value: {value}")
    index += 1

Output: Here, we have created a variable named index and set the value as 0. This will act as our index for the Python dictionary.

Every time we iterate over the key of the Python dictionary, we get the corresponding values with the index as we increment the index by 1.

Index: 0, Key: New York, Value: Albany
Index: 1, Key: California, Value: Sacramento
Index: 2, Key: Ohio, Value: Columbus
Index: 3, Key: Texas, Value: Austin
Index: 4, Key: Washington, Value: Olympia

Upon running the code in Pycharm, the resulting output is displayed in the screenshot below.

How to Iterate through a Dictionary in Python with Index

2. Python Iterate over dictionary with index using enumerate() function

The enumerate() function in Python is used to iterate over a sequence by index. Its syntax is:

enumerate(iterable, start=0)

It takes two arguments:

  1. iterable: Name of the iterable that we need to iterate over in Python
  2. start: (Integer) It is a counter from which indexing will start.

The enumerate() function yields a pair of the counter and element from the iterable.

This is how we can use the enumerate function to iterate through a Python dictionary with index:

USA_companies = {
    "Amazon": 299.28,
    "Apple": 297.51,
    "Google": 281.38,
    "Microsoft": 191.57,
    "Walmart": 113.78
}
for index, key in enumerate(USA_companies):
    print('Index::', index, ' Key::', key)

Output: By default, the start parameter of the enumerate function is set to 0.

Index:: 0  Key:: Amazon
Index:: 1  Key:: Apple
Index:: 2  Key:: Google
Index:: 3  Key:: Microsoft
Index:: 4  Key:: Walmart

Below is a screenshot that reveals the output after the code has been implemented in the Pycharm editor.

Python Iterate Dictionary with index

3. Iterate through the dictionary by indexes in Python using the items() method

The items() method of a Python dictionary returns a key-value pair that can be used in a loop for iterating through keys and values.

But for the index, we have to convert the return value of the items() method into a list and then iterate through the list in Python.

Let’s see an example of this:

US_States = {
    "Alaska": "AK",
    "California": "CA",
    "Florida": "FL",
    "Hawaii": "FL",
    "New York": "NY",
    "Texas": "TX"
}
Sates_data = list(US_States.items())
for i in range(len(Sates_data)):
    key, value = Sates_data[i]
    print(f'Index: {i}, Key: {key}, Value: {value}')

Output:

Index: 0, Key: Alaska, Value: AK
Index: 1, Key: California, Value: CA
Index: 2, Key: Florida, Value: FL
Index: 3, Key: Hawaii, Value: FL
Index: 4, Key: New York, Value: NY
Index: 5, Key: Texas, Value: TX

The screenshot below presents the output obtained after the code execution in the Pycharm editor.

Iterate over a dictionary in Python with index

4. Python Iterate dictionary by index using sorted() function

If we want to sort the dictionary in Python and iterate over the sorted dictionary in Python by index, then we can use the sorted() function.

The sorted() function returns the list of the sorted values in Python in ascending order.

Here is an example:

CEO_dict = {
    "Carnegie Steel Company": "Andrew Carnegie",
    "Ford Motor Company": "Henry Ford",
    "Oprah Winfrey Network": "Oprah Winfrey",
    "Microsoft": "Bill Gates",
    "Google": "Larry Page"
}
sorted_keys = sorted(CEO_dict.keys())
for i in range(len(sorted_keys)):
    key = sorted_keys[i]
    value = CEO_dict[key]
    print(f'Index: {i}, Key: {key}, Value: {value}')

Here, we have passed a list of keys to the sorted() function in Python to get the sorted list.

sorted(CEO_dict.keys())

Output:

Index: 0, Key: Carnegie Steel Company, Value: Andrew Carnegie
Index: 1, Key: Ford Motor Company, Value: Henry Ford
Index: 2, Key: Google, Value: Larry Page
Index: 3, Key: Microsoft, Value: Bill Gates
Index: 4, Key: Oprah Winfrey Network, Value: Oprah Winfrey

The resulting screenshot is provided below after the code has been implemented in the Pycharm editor.

Iterate through dictionary Python with index

Conclusion

This article explains “How to iterate through a dictionary in Python with index” in detail, in multiple ways. Whether we use a simple for loop, the enumerate() function, the items() methods, or the sorted() function for a sorted dictionary, understanding these methods allows programmers to work with dictionaries in Python effectively.

Choose the method that will best suit your requirements.

You may also like to read: