Python: Iterate Over Dictionary with Index

Dictionaries are the implementation by Python of a knowledge structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair.
The list of key value pairs in curly braces that’s separated by comma defines a dictionary. Column ‘:’ separates the value of each key.
A dictionary can’t be sorted only to urge a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore, you would like an ordered data type, which may be a list—probably an inventory of tuples.

Examples:

Input :

dictionary = {'This': 100, 'is':200, 'BTechGeeks':300}

Output:

index = 0  ; key = this  ; Value = 200
index = 1  ; key = is  ; Value = 100
index = 2  ; key = BTechGeeks  ; Value = 300

Traverse the Dictionary with Index

1)Enumerate() function:

When working with iterators, we frequently encounter the need to keep track of the number of iterations. Python makes it easier for programmers by providing a built-in function enumerate() for this purpose.
Enumerate() adds a counter to an iterable and returns it as an enumerate object. This enumerate object can then be utilized in for loops directly or converted into an inventory of tuples using the list() method.

Syntax:

enumerate(iterable, start=0)

Parameters:

iterable:  an iterator, a sequence, or objects that support iteration

start :   the index value from which the counter will be started; the default value is 0.

Return:

The method enumerate() adds a counter to an iterable and returns it. The object returned is an enumerate object.

2)Traverse over all key-value pairs of given dictionary by index

We can traverse over the dictionary by passing given dictionary as parameter in enumerate() function

Below is the implementation:

# given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# Traverse all key-value pairs of given dictionary by index
for i, key in enumerate(dictionary):
    print('index =', i, ' ; key =', key, ' ; Value =', dictionary[key])

Output:

index = 0  ; key = this  ; Value = 200
index = 1  ; key = is  ; Value = 100
index = 2  ; key = BTechGeeks  ; Value = 300

3)Traverse over all keys of given dictionary by index

The dictionary class’s keys() function returns an iterable sequence of all the dictionary’s keys. We can pass that to the enumerate() function, which will return keys as well as index position.

Below is the implementation:

# given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# Traverse all keys of given dictionary by index
for i, key in enumerate(dictionary.keys()):
    print('index =', i, ' ; key =', key)

Output:

index = 0  ; key = this
index = 1  ; key = is
index = 2  ; key = BTechGeeks

4)Traverse over all values of given dictionary by index

The dictionary class’s values() function returns an iterable sequence of all the dictionary’s values. We can pass that to the enumerate() function, which will return values as well as index position.

Below is the implementation:

# given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# Traverse all values of given dictionary by index
for i, value in enumerate(dictionary.values()):
    print('index =', i, ' ; value =', value)

Output:

index = 0  ; value = 200
index = 1  ; value = 100
index = 2  ; value = 300

Related Programs: