On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary.

The normal case you enumerate the keys of the dictionary:

Use with AIexample_dict = {1:'a', 2:'b', 3:'c', 4:'d'}

for i, k in enumerate(example_dict):
    print(i, k)

Which outputs:

Use with AI0 1
1 2
2 3
3 4

But if you want to enumerate through both keys and values this is the way:

Use with AIfor i, (k, v) in enumerate(example_dict.items()):
    print(i, k, v)

Which outputs:

Use with AI0 1 a
1 2 b
2 3 c
3 4 d
Answer from João Almeida on Stack Overflow
🌐
Pierian Training
pieriantraining.com › home › enumerating dictionaries in python
Enumerating Dictionaries in Python - Pierian Training
April 28, 2023 - To enumerate a dictionary using ... pairs as tuples. We can then iterate over this list using a for loop inside the list comprehension, and create a new list with the desired output....
Discussions

how to loop through dict with enumerate and unpack the index, key, and value
for i, (k, v) in enumerate(my_result.items()): print(v['charge_usage']) Your value v is just another dictionary. You can do the same things with it that you could do with any dictionary. More on reddit.com
🌐 r/learnpython
8
1
October 13, 2022
python - How to iterate `dict` with `enumerate` and unpack the index, key, and value along with iteration - Stack Overflow
How to iterate a dict with enumerate such that I could unpack the index, key and value at the time of iteration? ... I want to iterate through the keys and values in a dictionary called mydict and count them, so I know when I'm on the last pair. ... I'm printing the dict to a file, so I need to know when to insert a newline. ... Does the index matters for that? At the end of the loop ... More on stackoverflow.com
🌐 stackoverflow.com
python - Enumerate Dictionary Iterating Key and Value - Stack Overflow
I have a dictionary called regionspointcount that holds region names (str) as the keys and a count of a type of feature within that region (int) as the values e.g. {'Highland':21}. I am wanting to More on stackoverflow.com
🌐 stackoverflow.com
python - Iterating over a dictionary using a 'for' loop, getting keys - Stack Overflow
A pair of braces creates an empty ... to the dictionary. No, key is not a special word in Python. Here key is Just a variable name. ... Keys are immutable data types. Keys are unique. ... "Is key a special keyword, or is it simply a variable?" There are no such "special keywords" for for loop ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com › iterate-through-dictionary-python
How to Iterate Through a Dictionary in Python – Real Python
September 9, 2025 - For Python dictionaries, .__iter__() allows direct iteration over the keys by default. This means that if you use a dictionary directly in a for loop, Python will automatically call .__iter__() on that dictionary, and you’ll get an iterator that goes over its keys:
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-python-dictionary-using-enumerate-function
Iterate Python Dictionary Using Enumerate() Function - GeeksforGeeks
July 23, 2025 - In this example, the enumerate() function is used with the items() method of the dictionary, which returns key-value pairs. The loop unpacks each pair into variables key and value, while index keeps track of the iteration index.
🌐
Coursera
coursera.org › tutorials › how to enumerate in python: step by step
How to Enumerate in Python: Step by Step | Coursera
February 24, 2023 - The for loop unpacks each pair into i and fruit variables, where i is the index and fruit is the corresponding item. Try writing a program that returns the following output: ... 1 2 3 4 programs = ['python','c','react','javascript','bash'] for ...
🌐
Reddit
reddit.com › r/learnpython › how to loop through dict with enumerate and unpack the index, key, and value
r/learnpython on Reddit: how to loop through dict with enumerate and unpack the index, key, and value
October 13, 2022 -

an example of the dict

{10: {'total_power_usage': 152.1, 'total_pris': 336.62499999999994, 'huse_usage': 74.45588194444376, 'hus_pris': 195.42238509638779, 'charge_usage': 77.64411805555619, 'charge_price_full': 141.1951349036122, 'charge_price': 63.55101684805601}}

now i can do :

for i, (k, v) in enumerate(my_result.items()):
    print("index: {}, key: {}, value: {}".format(i, k, v))

and that prints each line in the dict just fine. But how do i access each named element in the dict? if i need the value from 'total_pris' or 'charge_usage' ?

🌐
Python Examples
pythonexamples.org › python-enumerate-a-dictionary
Enumerate a Dictionary
In this example, we will take a dictionary, and enumerate over the key:value pairs of dictionary in a For Loop. fruits = {'apple': 25, 'banana': 14, 'mango':48, 'cherry': 30} for x in enumerate(fruits.items()): print(x) (0, ('apple', 25)) (1, ('banana', 14)) (2, ('mango', 48)) (3, ('cherry', 30)) In this tutorial of Python Examples, we learned how to enumerate a dictionary using enumerate() builtin function.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › python › iterate over a dictionary in python
Iterate Over a Dictionary in Python with For Loops | Sentry
September 10, 2026 - This is useful if you need the position of the element in the dictionary along with the key-value pair: word_counts = {"the": 3, "a": 5, "be": 2, "do": 1} for index, (key, value) in enumerate(word_counts.items()): print(f"{index} {key}:{value}")
🌐
Towards Data Science
towardsdatascience.com › home › programming › python enumerate() function, explained
Python enumerate() Function, Explained | Towards Data Science
February 20, 2023 - When iterating over key-value pairs combined with enumerate() functionality, the output you will get is each index and a tuple containing the key-value pair at each entry in the dictionary: ... my_dict = { 'Apple': 3, 'Banana': 1, 'Orange': 2, 'Pineapple': 5 }for i, (key, value) in enumerate(my_dict.items()): print(i, (key, value)) ... A slightly more advanced example is using enumerate() with another Python function, for example .
🌐
freeCodeCamp
freecodecamp.org › news › what-is-enumerate-in-python
What is enumerate() in Python? Enumeration Example
December 22, 2022 - In our applications, we might want ... and then using that value to access another data structure. The most common way to utilize enumerate() is through a for loop....
🌐
Python Guides
pythonguides.com › python-for-loop-index
Python For Loop with Index (enumerate and start)
October 14, 2025 - No. Over two million items they take the same time. enumerate wins on readability and on removing a chance to index wrongly. Because looping a dictionary yields its keys. Use enumerate(d.items()) and unpack with for i, (key, value) in ...
🌐
freeCodeCamp
freecodecamp.org › news › dictionary-iteration-in-python
Dictionary Iteration in Python – How to Iterate Over a Dict with a For Loop
January 6, 2023 - Note: You can use any letter for the variable(s) in a for loop. It doesn't have to be k or v, or k, v. To convert a dictionary to a list of tuples in Python, you still have to use the items() method inside a for loop.
🌐
Simplilearn
simplilearn.com › home › resources › software development › an introduction to enumerate in python with syntax and examples
An Introduction to Enumerate in Python with Syntax and Examples
August 24, 2025 - Enumerate is a built-in function in the python library. This tutorial will help you to learn all about enumerate in python with syntax and examples. Start now!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Top answer
1 of 16
7030

key is just a variable name.

Use with AIfor key in d:

will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following:

For Python 3.x:

Use with AIfor key, value in d.items():

For Python 2.x:

Use with AIfor key, value in d.iteritems():

To test for yourself, change the word key to poop.

In Python 3.x, iteritems() was replaced with simply items(), which returns a set-like view backed by the dict, like iteritems() but even better. This is also available in 2.7 as viewitems().

The operation items() will work for both 2 and 3, but in 2 it will return a list of the dictionary's (key, value) pairs, which will not reflect changes to the dict that happen after the items() call. If you want the 2.x behavior in 3.x, you can call list(d.items()).

2 of 16
569

It's not that key is a special word, but that dictionaries implement the iterator protocol. You could do this in your class, e.g. see this question for how to build class iterators.

In the case of dictionaries, it's implemented at the C level. The details are available in PEP 234. In particular, the section titled "Dictionary Iterators":

  • Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. [...] This means that we can write

    Use with AIfor k in dict: ...
    

    which is equivalent to, but much faster than

    Use with AIfor k in dict.keys(): ...
    

    as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.

  • Add methods to dictionaries that return different kinds of iterators explicitly:

    Use with AIfor key in dict.iterkeys(): ...
    
    for value in dict.itervalues(): ...
    
    for key, value in dict.iteritems(): ...
    

    This means that for x in dict is shorthand for for x in dict.iterkeys().

In Python 3, dict.iterkeys(), dict.itervalues() and dict.iteritems() are no longer supported. Use dict.keys(), dict.values() and dict.items() instead.

🌐
StrataScratch
stratascratch.com › blog › how-to-iterate-over-a-dictionary-in-python
How to Iterate Over a Dictionary in Python? - StrataScratch
December 11, 2025 - Let’s take a simple example of ... 1, 'b': 300, 'c': 2, 'e': 400, 'd': 0} ... The approach to sort on keys is given by a simple for loop combined with the sorted() function....
🌐
Note.nkmk.me
note.nkmk.me › home › python
Iterate Over Dictionary Keys, Values, and Items in Python | note.nkmk.me
April 24, 2025 - You can iterate over dictionary keys simply by using the dictionary itself in a for loop. To get a list of all its keys, simply pass a dictionary to list(). for k in d: print(k) # key1 # key2 # key3 print(list(d)) # ['key1', 'key2', 'key3'] ...