Enumerating a dictionary in Python
The function in Python enumerate()
returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier.
When we pass an enumeration object to list()
a function, we can inspect its contents. For example:
l = ["a", "b", "c"]
print(list(enumerate(l)))
Output:
[(0, 'a'), (1, 'b'), (2, 'c')]
We can also use enumerate()
the function with dictionaries.
The following example shows a basic example.
d1 = {"a": 15, "b": 18, "c": 20}
for i, j in enumerate(d1):
print(i, j)
Output:
0 a
1 b
2 c
Notice that we passed the dictionary directly to enumerate()
the function, which only assigned the counter variable to the dictionary's keys and not the values. So when we iterate over this object, we can only access the counter variable and the dictionary's keys.
To enumerate both keys and values at the same time, we can use the dictionary items()
method. items()
The method returns an object with a tuple of key-value pairs. The following example shows how we can use items()
the method and enumerate()
the function to access both a key and its corresponding value.
d1 = {"a": 15, "b": 18, "c": 20}
for i, (j, k) in enumerate(d1.items()):
print(i, j, k)
Output:
0 a 15
1 b 18
2 c 20
If we want only the dictionary elements without the key, we can use values()
the function. It returns a list containing the dictionary values.
The following code shows how to do it.
d1 = {"a": 15, "b": 18, "c": 20}
for i, j in enumerate(d1.values()):
print(i, j)
Output:
0 15
1 18
2 20
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing dictionary values in Python
Publish Date:2025/05/05 Views:108 Category:Python
-
This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary
Finding the maximum value in a Python dictionary
Publish Date:2025/05/05 Views:60 Category:Python
-
This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t
How to read input from stdin in Python
Publish Date:2025/05/05 Views:124 Category:Python
-
This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P
Maximum integer in Python
Publish Date:2025/05/05 Views:55 Category:Python
-
This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t
Get a list of time zones using Python
Publish Date:2025/05/05 Views:107 Category:Python
-
When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around
Convert NumPy array to list in Python
Publish Date:2025/05/05 Views:101 Category:Python
-
Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si
Calculating Mahalanobis distance in Python
Publish Date:2025/05/05 Views:185 Category:Python
-
This tutorial will show you how to find the Mahalanobis distance between two NumPy arrays in Python. Use the function scipy.spatial.distance in the Python library cdist() to calculate the Mahalanobis distance The Mahalanobis distance is a m