Python loop over dict – Python: Iterate Over Dictionary (All Key-Value pairs)

Python loop over dict: Python’s implementation of an associative array, which is a data structure, is dictionaries. A dictionary is a collection of key-value pairs. Each key-value pair represents a key and its associated value.

Enclosing a comma-separated list of key-value pairs in curly braces defines a dictionary { }. A colon ‘ : ‘ separates each key from its associated value.

Note:

  • Keys are mapped to values in dictionaries, which are then stored in an array or series.
  • The keys must be of the hashable form, which means that their hash value must remain constant over their lifetime.

The keys and values of a dictionary are iterated over in the same order as they were generated in Python 3.6 and later. However, this behaviour varies between Python versions and is dependent on the dictionary’s insertion and deletion history.

Examples:

Input :

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

Output:

This 100
is 200
BTechGeeks 300

Traverse the dictionary

Iterate over key value pairs python: There are several ways to traverse the dictionary some of them are:

Method #1: Using for loop

Iterate over dictionary python: To iterate over all keys in a dictionary, a dictionary object may also be used as an iterable object. As a result, we can apply for loop on a dictionary with ease. It loops through all the keys in the dictionary by using for in the dictionary. We will pick the value associated with each key and print it.

Below is the implementation:

# Given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# Using for loop to traverse the dictionary
for key in dictionary:
    # here key gives the key of the dictionary
    # Getting value at key
    value = dictionary[key]
    # printing values and keys
    print(key, value)

Output:

This 100
is 200
BTechGeeks 300

Method #2:Using items()

Iterate over keys and values python: When dealing with dictionaries, you’ll almost certainly want to use both the keys and the values. .items(), a method that returns a new view of the dictionary’s items, is one of the most useful ways to iterate through a dictionary in Python.

This sequence is an iterable View object that contains all of the dictionary’s key,value elements. It is supported by the original dictionary. Let’s use this to iterate over all of the dictionary’s key-value pairs.

Below is the implementation:

# Given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# Using items and converting dictionary to list
dictlist = list(dictionary.items())
# Traverse the dictlist and print key and values of dictionary
for i in dictlist:
  # printing key and value
    print(i[0], i[1])

Output:

This 100
is 200
BTechGeeks 300

Method #3:Using List Comprehension

Python iterate over a dictionary: Since the items() function of a dictionary returns an iterable sequence of key-value pairs, we may use this list comprehension to iterate over all diction pairs.

Below is the implementation:

# Given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# Using list comprehension
[print(key, value) for key, value in dictionary.items()]

Output:

This 100
is 200
BTechGeeks 300

Filtering Items in dictionary

Python for loop over dictionary: You can find yourself in a situation where you have an existing dictionary and want to construct a new one to store only the data that meets a set of criteria. This can be accomplished using an if statement inside a for loop, as shown below:

# Given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# Create a new empty dictionary
newdictionary = dict()
# Traverse the original dictionary and check the condition
for key, value in dictionary.items():
    # If value meets the criteria, it should be saved in new dict.
    if value <= 200:
        newdictionary[key] = value
# printing the new dictionary
print(newdictionary)

Output:

{'This': 100, 'is': 200}

Related Programs: