Python: Iterate over dictionary with index

In this article, we will discuss different ways to iterate over a dictionary by index.

Table of Contents

For iterating over a sequence by indexing, we can use the function enumerate() in python.

Overview of enumerate():

The enumerate() function in python provides a way to iterate over a sequence by index.

enumerate(iterable, start=0)

It accepts two arguments:

  • iterable: An iterable sequence over which we need to iterate by index.
  • start: An int value.
    • It is the counter from which indexing will start.

It yields a pair of counter and item from the iterable. We can use it to iterate over the iterable sequence and but now along with item we will get the index position of the item while iteration.

Let’s use this to iterate over a dictionary by index,

Iterate over all key-value pairs of dictionary by index

The items() function of dictionary class returns an iterable sequence of all key value pairs of the dictionary. We can pass that to the enumerate() function to yields key-value pairs along with index position. For example,

# A dictionary of string and integers
word_freq = {
    'Hello' : 56,
    "at"    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Iterate over all key-value pairs of dictionary by index
for index, (key, value) in enumerate(word_freq.items()):
    print('Index:: ', index, ' :: ', key, '-', value)

Output:

Index::  0  ::  Hello - 56
Index::  1  ::  at - 23     
Index::  2  ::  test - 43   
Index::  3  ::  This - 78   
Index::  4  ::  Why - 11 

As we passed the sequence returned by items() to the enumerate() function with start index 0 (default value). Therefore it yielded each item (key-value) of dictionary along with index, starting from 0.

Iterate over all keys of dictionary by index

The keys() function of dictionary class returns an iterable sequence of all keys of the dictionary. We can pass that to the enumerate() function to yields keys along with index position. For example,

# A dictionary of string and integers
word_freq = {
    'Hello' : 56,
    "at"    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Iterate over all keys of dictionary by index
for index, key in enumerate(word_freq):
    print('Index:: ', index, ' :: ', key)

Output:

Index::  0  ::  Hello       
Index::  1  ::  at
Index::  2  ::  test
Index::  3  ::  This
Index::  4  ::  Why

As we passed the sequence returned by keys() to the enumerate() function with start index 0 (default value). Therefore it yielded each key of the dictionary along with index, starting from 0.

While iterating over all keys with index position, we can also fetch the value associated with the key. For example,

# A dictionary of string and integers
word_freq = {
    'Hello' : 56,
    "at"    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Iterate over all key-value pairs of dictionary by index
for index, key in enumerate(word_freq):
    print('Index:: ', index, ' :: ', key, ' , ', word_freq[key])

Output:

Index::  0  ::  Hello  ,  56
Index::  1  ::  at  ,  23
Index::  2  ::  test  ,  43
Index::  3  ::  This  ,  78
Index::  4  ::  Why  ,  11

Iterate over all values of dictionary by index

The values() function of dictionary class returns an iterable sequence of all values of the dictionary. We can pass that to the enumerate() function to yields values along with index position. For example,

# A dictionary of string and integers
word_freq = {
    'Hello' : 56,
    "at"    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Iterate over all values of dictionary by index
for index, value in enumerate(word_freq.values()):
    print('Index:: ', index, ' :: ', value)

Output:

Index::  0  ::  56
Index::  1  ::  23
Index::  2  ::  43
Index::  3  ::  78
Index::  4  ::  11

As we passed the sequence returned by values() to the enumerate() function with start index 0 (default value). Therefore it yielded each value of the dictionary along with index, starting from 0.

Conclusion:

We learned, how to iterate over a dictionary in python with indexing. We can either iterate over all key-value pairs or only keys or values with indices.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top