How to loop over a dictionary using Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Dictionary cannot be looped over like usual lists. Since they contain pair:value elements you will need to go through each key to loop over a dictionary.

Here is how to loop over a dictionary in Python.

Using the keys

Using the .keys() method you will loop over the keys.

In order to get the value corresponding to the pair you will need to use the key at every step. (e.g. my_dict[key])

my_dict = {
	"firstname":"John",
    "lastname":"Smith",
    "age":34,
}

# We loop over the dict using keys
for key in my_dict.keys():
	print(key, my_dict[key])
How to loop over a dictionary using keys

Using items

Using items will get you a tuple (key, value) for every key:value pair.

Which you can print directly as an element if written like so:

my_dict = {
	"firstname":"John",
    "lastname":"Smith",
    "age":34,
}

# We loop over the dict using items
for key, value in my_dict.items():
	print(key, value)
How to loop over a dictionary using items

Using values

You might end up needing the values only. The values() method is the one to go as it will directly give you a list of all the values contained in the dictionary.

my_dict = {
	"firstname":"John",
    "lastname":"Smith",
    "age":34,
}

# We loop over the dict using values
for value in my_dict.values():
	print(value)
How to loop over a dictionary using values

Here you are! You now know how to loop over a dictionary in Python.

More on fundamentals

If you want to know more about Python fundamentals without headaches... check out the other articles I wrote by clicking just here:

Fundamentals - The Python You Need
We gathered the only Python essentials that you will probably ever need.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.