In this article, we will discuss different ways to iterate over a dictionary by index.
Table of Contents
- Overview of enumerate()
- Iterate over all key-value pairs of dictionary by index
- Iterate over all keys of dictionary by index
- Iterate over all values of dictionary by index
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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.