key is just a variable name.

for 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:

for key, value in d.items():

For Python 2.x:

for 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()).

Answer from sberry on Stack Overflow
Top answer
1 of 16
7028

key is just a variable name.

for 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:

for key, value in d.items():

For Python 2.x:

for 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
568

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

    for k in dict: ...
    

    which is equivalent to, but much faster than

    for 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:

    for 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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-dictionary-in-python
Iterate Over a Dictionary in Python - GeeksforGeeks
July 16, 2026 - items() method returns both the key and its corresponding value together. This is useful when both pieces of information are needed during iteration. ... Explanation: items() returns each dictionary entry as a (key, value) pair, which is unpacked ...
Discussions

Iterate through a Dictionary
loop over both at once using .items() for key,value in ngrams.items(): More on reddit.com
🌐 r/learnpython
14
1
September 10, 2019
What is the fastest way to iterate over a dictionary?
try again but this time do "for key, vals in dictName.items()" first and "for vals in dictName.values():" second see if your times still hold More on reddit.com
🌐 r/learnpython
17
99
January 4, 2023
Why do you need to apply the method .items() to iterate over a dictionary?
You don't. items() will give you (key, value) pairs. Naked iteration over a dict will give you the keys. >>> d = {1: 'a', 2: 'b', 3: 'c'} >>> for key, value in d.items(): ... print(f"{key}:{value}") ... 1:a 2:b 3:c >>> for key in d: ... print(key) ... 1 2 3 >>> More on reddit.com
🌐 r/learnpython
5
4
November 18, 2020
How to slice a dictionary
What do you mean exactly by "slice a dictionary"? If you're talking about list slicing, you can't. What do you mean by "get 1" from {‘a’:1, ‘b’:2, ‘c’:3}? You can lookup the value of the key 'a', eg. d = {‘a’:1, ‘b’:2, ‘c’:3} print(d['a']) What do you mean by "get 'a'"? In this dictionary 'a' is a key. You can iterate over the keys of a dictionary by just using the dict as an iterator in the usual way, eg. d = {‘a’:1, ‘b’:2, ‘c’:3} for key in d: print(key) More on reddit.com
🌐 r/learnpython
4
1
February 26, 2021
🌐
Note.nkmk.me
note.nkmk.me › home › python
Iterate Over Dictionary Keys, Values, and Items in Python | note.nkmk.me
April 24, 2025 - The values() method returns dict_values, which can be converted to a list using list(). print(d.values()) # dict_values([1, 2, 3]) print(type(d.values())) # <class 'dict_values'> ... To iterate over dictionary key-value pairs, use the items() method.
🌐
Real Python
realpython.com › iterate-through-dictionary-python
How to Iterate Through a Dictionary in Python – Real Python
September 9, 2025 - This is the primary way to iterate through a dictionary in Python. You just need to put the dictionary directly into a for loop, and you’re done! If you use this approach along with the [key] operator, then you can access the values of your dictionary while you loop through the keys:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-iterate-dictionary-key-value
Python Iterate Dictionary Key, Value - GeeksforGeeks
July 23, 2025 - #function to iterate over each keys and its #corresponding values def dict_iter(d): for i in d: print('KEYS: {} and VALUES: {}'.format(i,d[i])) #Main Function if __name__ == &quot;__main__&quot;: d = {&quot;Vishu&quot;:1,&quot;Aayush&quot;:2,&quot;Neeraj&quot;:3,&quot;Sumit&quot;:4} #calling function created above dict_iter(d) ... KEYS: Vishu and VALUES: 1 KEYS: Aayush and VALUES: 2 KEYS: Neeraj and VALUES: 3 KEYS: Sumit and VALUES: 4 · In this example we will be using Python's dictionary function .items() .
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to iterate over a dictionary in python ?
How to Iterate Over a Dictionary in Python? - Analytics Vidhya
February 7, 2025 - The keys() method is utilized in the for loop to iterate through each key (fruit) in the dictionary. Inside the loop, we access the corresponding value using the key and print the fruit along with its price.
🌐
W3Schools
w3schools.com › Python › python_dictionaries_loop.asp
Python - Loop Dictionaries
You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.
Find elsewhere
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to iterate over keys and values in python dictionaries
How To Iterate Over Keys and Values in Python Dictionaries | Towards Data Science
January 19, 2025 - Now in case you need to iterate over both keys and values in one go, you can call [items()](https://docs.python.org/3/library/stdtypes.html#dict.items). The method will return a tuple containing the key value pairs in the form (key, value).
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-through-dictionary-keys-and-values-in-python
Iterate Through Dictionary Keys And Values In Python - GeeksforGeeks
July 23, 2025 - The for loop in list comprehension iterates through all the keys and values and prints all the key-value pairs in a dictionary format. ... # defining dictionary related to GeeksforGeeks geeks_data = {'language': 'Python', 'framework': 'Django', ...
🌐
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 - In this tutorial, we looked at how to iterate through a dictionary with the for loop. If you don’t want to use a for loop, you can also use any of the keys(), values(), or items() methods directly like I did in the first part of this article.
🌐
Sentry
sentry.io › sentry answers › python › iterate over a dictionary in python
Iterate over a dictionary in Python | Sentry
January 30, 2023 - While this works, we may prefer to iterate over keys and values at the same time. Python’s built-in dict class includes a method called items(), which returns a dictionary view object that can be used for exactly this purpose.
🌐
Python Central
pythoncentral.io › how-to-iterate-through-a-dictionary-in-python
How to Iterate Through a Dictionary in Python | Python Central
November 19, 2024 - for key, value in person.items(): if isinstance(value, dict): ... name: John Doe age: 35 occupation: Software Engineer address: street: 123 Main St city: Anytown state: CA · In this example, we first check if the value is a dictionary. If so, we iterate through the nested dictionary and print the key-value pairs.
🌐
Reddit
reddit.com › r/learnpython › iterate through a dictionary
r/learnpython on Reddit: Iterate through a Dictionary
September 10, 2019 -

I’m stumped on how to iterate through a dictionary and compare each keys value to one another. I’m able to compare the first key value to the rest of the keys in the dictionary but after that, comparing say the third key to the first key all in the same loop seems impossible to me as of now.

I’m sure it all goes in a loop starting with:

for keys in ngrams.keys():

basically I’m trying to pull the keys with values who equal the highest (simplified version) and grab those two keys from the rest.

Any tips would be appreciated!

🌐
Python Shiksha
python.shiksha › home › tips › iterate through a python dictionary key values using "for" loop
Iterate through a Python Dictionary key values using "for" loop - Python Shiksha
July 18, 2021 - If there is a need to extract only the keys from a dictionary, then we can simply iterate through the keys by calling the .keys() method which gives us a dynamic view object of all the keys of that dictionary.
🌐
JanBask Training
janbasktraining.com › community › python-python › iterating-over-dictionary-in-python-and-using-each-value
Iterating over dictionary in Python and using each value | JanBask Training Community
September 21, 2025 - Iterating over key-value pairs: for key, value in my_dict.items(): is the most Pythonic way, since it directly gives you both elements. ... Efficient looping: Using .items() is often the best choice because it avoids repeatedly looking up values.
🌐
Career Karma
careerkarma.com › blog › python › iterate through dictionary python: step-by-step guide
Iterate Through Dictionary Python: Step-By-Step Guide | Career Karma
December 1, 2023 - The words on the left of the colons ... as strings. You can iterate through a Python dictionary using the keys(), items(), and values() methods....
🌐
PythonForBeginners
pythonforbeginners.com › home › iterating over dictionary in python
Iterating over dictionary in Python - PythonForBeginners.com
December 3, 2021 - The dictionary is: {'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'} The values in the dictionary are: PythonForBeginners PFB Python Tutorials Website · In the code above, we have simply obtained an iterator which iterates over the keys in the and then we have accessed the values associated to the keys using the syntax dict_name[key_name] and then the values are printed.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python iterate over a dictionary
Python Iterate Over A Dictionary - Spark By {Examples} %
May 31, 2024 - How to iterate Python Dictionary using for loop? You can iterate a dictionary in python over keys, iterate over the key and the value, using the lambda
🌐
TechBeamers
techbeamers.com › how-to-iterate-through-a-dictionary-python
Iterate Through a Dictionary in Python - TechBeamers
November 30, 2025 - The following are different ways to loop through a dictionary in Python. Please go through each of them and choose the one that fits the most in your case. One of the simplest ways to iterate through a dictionary is by accessing its keys.
🌐
Kodeclik
kodeclik.com › python-iterate-through-dictionary
How to iterate through a Python dictionary
July 20, 2025 - The first approach we will use uses the keys() method to loop over the dictionary. Here is how that works: mydict = dict(Apples=2.5, Oranges=5.5, Bananas=1.2) for key in mydict.keys(): print(key)