Use for loop for iteration.

my_dict = {"a": 1, "b": 2, "c": 3}
for key, value in my_dict.items():
    print(key + " " + str(value))

for key in my_dict:
    print(key + " " + str(my_dict[key]))

The first one iterates over items and gives you keys and values. The second one iterates over keys and then it is accessing value from the dictionary using the key.

Answer from fiveobjects on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_dictionaries_loop.asp
Python - Loop Dictionaries
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... You can loop through a dictionary by using a for loop.
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-dictionary-in-python
Iterate Over a Dictionary in Python - GeeksforGeeks
July 16, 2026 - Iterating over it directly returns its keys, which can then be used to access the corresponding values. ... Explanation: When a dictionary is used directly in a for loop, each iteration returns a key.
🌐
Reddit
reddit.com › r/learnpython › what is the fastest way to iterate over a dictionary?
r/learnpython on Reddit: What is the fastest way to iterate over a dictionary?
January 4, 2023 -

I was iterating over a dictionary, (more specifically a Counter object) and only needed the values, so I did something like: "for vals in dictName.values():"

However, I got a relatively slow runtime and was trying out different things, and then I tried: "for key, vals in dictName.items()" and the runtime was halved.

So, my question is: Why is one way faster than the other? And how is it working internally?

If someone can shed some light or point me in the direction of documentation explaining this speedup, I would be grateful. Thank you

Top answer
1 of 10
36
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
2 of 10
29
Could you post your code? That seems very surprising - if there was a difference at all, I'd expect it to be .items() that was slower, since it's bringing back more stuff and has to build a tuple. Some quick timing seems to bear this out: d={k:k for k in range(1000)} %timeit list(d.items()) 24.6 µs ± 311 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) %timeit list(d.values()) 5.81 µs ± 17.4 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) In fact, items() seems significantly slower than I'd expect. I also tried with a for loop rather than using list to exhaust in case that was introducing something, and the results aren't as big, but in the same direction: %timeit for x in d.values(): pass 10.2 µs ± 1.22 µs per loop (mean ± std. dev. of 7 runs, 100,000 loops each) %timeit for x in d.items(): pass 18.3 µs ± 96.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) So items is nearly twice as slow (I suspect the main difference between this and the list() test is the proportion of other code involved: we only see such an effect because we're doing virtually nothing, so the proportion involved is much larger than it'd be in a real program. As such, I suspect you're doing something wrong, and my first guess to what that is would be to check: are you actually iterating through the returned object. Ie. if you're just timing the .items() call, you're not checking the right thing, since these are lazy - they just return a view on the object and won't do any iteration until you actually start reading from the iterator. As such, they'll just take a constant time unrelated to the size of your dict etc, and unrepresentative of what happens when you iterate.
🌐
CodingNomads
codingnomads.com › python-iterate-dictionary
Python: Iterate Dictionary
Python Statements ... Create a free account to track your progress, so you won't miss a thing. Join for free · Access all coding, AI, and data science courses, plus videos, IDEs, interactive lessons, and Discord support! Learn more ... Dictionaries are iterable mappings, meaning you can iterate over them similar to other collections, such as a string, a tuple, a list, or a set.
🌐
Real Python
realpython.com › iterate-through-dictionary-python
How to Iterate Through a Dictionary in Python – Real Python
September 9, 2025 - By understanding these techniques, ... You can directly iterate over the keys of a Python dictionary using a for loop and access values with dict_object[key]....
Find elsewhere
🌐
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 - How to Iterate through Dictionary Items with a for Loop · How to Loop through a Dictionary and Convert it to a List of Tuples ... With the Python for loop, you can loop through dictionary keys, values, or items. You can also loop through the ...
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.7 documentation
Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section. Because sets are unordered, iterating over them or printing them can produce the elements in a different order than you expect.
🌐
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 - By using keys(), you efficiently ... with the keys themselves. Utilizing the values() method in Python provides a straightforward approach to iterate through all the values of a dictionary....
🌐
YouTube
youtube.com › real python
How to Iterate Through a Dictionary in Python
Sorry for the interruption. We have been receiving a large volume of requests from your network · To continue with your YouTube experience, please fill out the form below
Published: December 26, 2019
Views: 12K
🌐
Cisco
ipcisco.com › home › python iterate dictionary
Python Iterate Dictionary | How To Use "For Loop" With Dictionary⋆
March 5, 2026 - We use for loops on Python dictionaries like python lists and python tuples.
🌐
Sentry
sentry.io › sentry answers › python › iterate over a dictionary in python
Iterate over a dictionary in Python | Sentry
January 30, 2023 - The following code will print each key in a dictionary with its corresponding value: word_counts = {"the": 3, "a": 5, "be": 2, "do": 1} for key in word_counts: print(f"{key}: {word_counts[key]}") 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.
🌐
YouTube
youtube.com › watch
Iterate Dictionary Items in Python | For Loop Technique
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Python Morsels
pythonmorsels.com › looping-over-dictionaries
Looping over dictionaries - Python Morsels
March 6, 2023 - Using the items method is probably the most common way to loop over a dictionary in Python. For consistency, dictionaries also have a keys method, which returns an iterable of just the keys:
🌐
StrataScratch
stratascratch.com › blog › how-to-iterate-over-a-dictionary-in-python
How to Iterate Over a Dictionary in Python? - StrataScratch
December 11, 2025 - We will start with a quick refresher on the foundational capabilities of Python dictionaries and then quickly advance into the methods for iteration, from writing loops to ‘Pythonic’ approaches like dict comprehensions, and finally, the broad concept of iterators and iterables that can be applied not just to dictionaries, but more broadly for your programming needs.
🌐
Stack Overflow
stackoverflow.com › questions › 76417338 › how-to-iterate-over-a-dictionary-and-access-both-keys-and-values-in-python
How to iterate over a dictionary and access both keys and values in Python? - Stack Overflow
I would appreciate any code examples or suggestions on the most efficient and Pythonic way to accomplish this! ... Save this answer. ... Show activity on this post. ... Sign up to request clarification or add additional context in comments. ... Iterating over my_dict.items() is a bit faster, especially for larger dictionaries.
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-through-specific-keys-in-a-dictionary-in-python
Iterate Through Specific Keys in a Dictionary in Python - GeeksforGeeks
July 23, 2025 - d = {"a": 1, "b": 2, "c": 3, "d": ... in keys if key in d} print(new) ... The filter() function is used to create an iterator of keys from the list keys that also exist in dictionary d....
🌐
PYnative
pynative.com › home › python exercises › python basic exercise for beginners: 40 coding problems with solutions
Python Basic Exercise for Beginners: 40 Coding Problems with Solutions
February 8, 2026 - You can solve this using a for loop with a range that steps by 2. Iterate through the characters of the string using a loop and the range() function. Use start = 0, stop = len(s) - 1, and step = 2. The step is 2 because we want only even index numbers. Or by using Python’s built-in string ...
🌐
TestMu AI Community
community.testmuai.com › ask a question
How does Python iterate over dictionary keys in a 'for' loop? - Ask a Question - TestMu AI (formerly LambdaTest) Community
November 10, 2024 - How does Python recognize that it needs only to read the key from the dictionary when using a ‘for’ loop to python iterate over dictionary? In the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print(k…