How to iterate through a dictionary in Python?


Dictionaries are a valuable and frequently used data structure in Python. This article tells us how to traverse through a dictionary while performing operations on its key-value pairs.

Using dict.items() Method

Python's dict.items() method allows you to loop through the dictionary. Each repetition will provide you with both the key and value of each item.

Example

Following is an example to iterate through a dictionary using dict.items() method −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for keys, values in dictionary.items(): print(keys ,values)

Output

Following is an output of the above code.

Novel Pride and Prejudice
year 1813
author Jane Austen
character Elizabeth Bennet

Using Keys() Method

To iterate through the dictionary's keys, utilise the keys() method that is supplied by the dictionary. An iterable of the keys available in the dictionary is returned. Then, as seen below, you can cycle through the keys using a for loop.

Example - 1

Following is an example to iterate through a dictionary using keys() method −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for keys in dictionary.keys(): print(keys)

Output

Following is an output of the above code:.

Novel
year
author
character

Example - 2

If you want to get the values of the keys during each iteration, you can use the get() method, as it is demonstrated below −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for keys in dictionary.keys(): print(dictionary.get(keys))

Output

Following is an output of the above code.

Pride and Prejudice
1813
Jane Austen
Elizabeth Bennet

Using Values() Method

To iterate through the values of the dictionary elements, utilise the values() method that the dictionary provides.

An iterable of all the values for each item that is available in the dictionary is returned. You can then go through the numbers as shown below by using a for loop.

Example

This approach prevents access to the dictionary keys(), which is typically not required. As a result, iterating through the dictionary using this way is the quickest method.

The items() method offered by the Python dictionary allows you to loop through the dictionary entries.

Following is an example to iterate through a dictionary using values() method −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for values in dictionary.values(): print(values)

Output

Following is an output of the above code −

Pride and Prejudice
1813
Jane Austen
Elizabeth Bennet

Iterating with Index

The items' index can be used to iterate across the dictionary.Iterating the dictionary without utilising the methods keys(), values(), or items is similar to this.

Example

Following is an example to iterate through a dictionary with index −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for index in dictionary: print(index, dictionary[index])

Output

Following is an output of the above code.

Novel Pride and Prejudice
year 1813
author Jane Austen
character Elizabeth Bennet

Iterating Over Dictionary In Alphabetical Order

Ordinarily, dictionaries don't keep any sort of order. This implies that the iteration's order of the items is not guaranteed. The sorted() function in Python can be used to iterate a dictionary using a given order. The item will be sorted first, after which a for loop can traverse over it.

Example

sorted(dictionary.keys()) sorts the dictionary's keys and for iterates through the keys that the sorted function returned.

Following is an example to iterate through a dictionary with index −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for keys in sorted(dictionary.keys()): print(keys, dictionary[keys])

Output

Following is an output of the above code.

Novel Pride and Prejudice
author Jane Austen
character Elizabeth Bennet
year 1813

Sort Using Dictionary Item Values

You must first generate a sorted set of keys before you can sort the dictionary according to its values.

The sorted keys set can then be iterated, with each iteration allowing you to access the dictionary using the key.

Example

A set of sorted keys is produced using sorted keys = sorted(dictionary,key=dictionary.get).

in sorted keys for values: - repeats the set of sorted keys

sorted_dictionary[values] = dictionary[values]–To the sorted dictionary, access the dictionary and add the value. Now, the sorted values will be present in the final dictionary. To verify the outcomes, print it.

Following is an example to sort using dictionary item values −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } sorted_dictionary = {} sorted_key = sorted(dictionary, key=dictionary.get) for values in sorted_key: sorted_dictionary[values] = dictionary[values] print(sorted_dictionary)

Output

Following is an output of the above code −

{'year': '1813', 'character': 'Elizabeth Bennet', 'author': 'Jane Austen', 'Novel': 'Pride and Prejudice'}

Updated on: 18-Aug-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements