- Iterate dictionary (key and value) with for loop in Python
- Iterate dictionary keys: keys()
- Iterate dictionary values: values()
- Iterate dictionary key-value pairs: items()
- Dictionary Iteration in Python – How to Iterate Over a Dict with a For Loop
- What We’ll Cover
- How to Iterate through a Dictionary with a for Loop
- How to Iterate through Dictionary Keys with a for Loop
- How to Iterate through Dictionary Values with a for Loop
- How to Iterate through Dictionary Items with a for Loop
- How to Loop through a Dictionary and Convert it to a List of Tuples
- Conclusion
Iterate dictionary (key and value) with for loop in Python
In Python, to iterate through a dictionary ( dict ) with a for loop, use the keys() , values() , and items() methods. You can also get a list of all keys and values in the dictionary with those methods and list() .
Consider the following dictionary as an example:
You can iterate keys by directly using the dictionary object in a for loop.
for k in d: print(k) # key1 # key2 # key3
See the following article for the basics of for loops in Python.
Iterate dictionary keys: keys()
As mentioned above, you can iterate dictionary keys by directly using the dictionary object, but you can also use keys() . The result is the same, but keys() may clarify the intent to the reader of the code.
for k in d.keys(): print(k) # key1 # key2 # key3
The keys() method returns dict_keys , which can be converted to a list with list() .
keys = d.keys() print(keys) print(type(keys)) # dict_keys(['key1', 'key2', 'key3']) # k_list = list(d.keys()) print(k_list) print(type(k_list)) # ['key1', 'key2', 'key3'] #
You can use dict_keys to perform set operations. See the following article for details.
Iterate dictionary values: values()
To iterate dictionary values, use the values() method.
for v in d.values(): print(v) # 1 # 2 # 3
The values() method returns dict_values , which can be converted to a list with list() .
values = d.values() print(values) print(type(values)) # dict_values([1, 2, 3]) # v_list = list(d.values()) print(v_list) print(type(v_list)) # [1, 2, 3] #
Iterate dictionary key-value pairs: items()
To iterate dictionary key-value pairs, use the items() method.
for k, v in d.items(): print(k, v) # key1 1 # key2 2 # key3 3
You can also receive the key-value pairs as a tuple of (key, value) :
for t in d.items(): print(t) print(type(t)) print(t[0]) print(t[1]) print('---') # ('key1', 1) # # key1 # 1 # --- # ('key2', 2) # # key2 # 2 # --- # ('key3', 3) # # key3 # 3 # ---
The items() method returns dict_items , which can be converted to a list with list() .
items = d.items() print(items) print(type(items)) # dict_items([('key1', 1), ('key2', 2), ('key3', 3)]) # i_list = list(d.items()) print(i_list) print(type(i_list)) # [('key1', 1), ('key2', 2), ('key3', 3)] # print(i_list[0]) print(type(i_list[0])) # ('key1', 1) #
You can also use dict_items to perform set operations. See the following article for details.
Dictionary Iteration in Python – How to Iterate Over a Dict with a For Loop
Kolade Chris
In Python, a dictionary is one of the built-in data structures (the others are tuples, lists, and sets). A dictionary is a collection of key:value pairs and you can use them to solve various programming problems.
Dictionaries are very flexible to work with. You can get the keys and values separately, or even together.
This article is about looping over a dictionary with the for loop, but you can also loop through a dictionary with three methods:
- the key() method: gets you the keys in a dictionary
- the values() method: gets you the values in a dictionary
- the items() method: gets you both the keys and values in a dictionary
In the example below, I use those 3 methods to get the keys, values, and items of the dictionary.
states_tz_dict = < 'Florida': 'EST and CST', 'Hawaii': 'HST', 'Arizona': 'DST', 'Colorado': 'MST', 'Idaho': 'MST and PST', 'Texas': 'CST and MST', 'Washington': 'PST', 'Wisconsin': 'CST' ># Keys states_keys = states_tz_dict.keys() print(states_keys) # dict_keys(['Florida', 'Hawaii', 'Arizona', 'Colorado', 'Idaho', 'Texas', 'Washington', 'Wisconsin']) # Values tz_values = states_tz_dict.values() print(tz_values) # dict_values(['EST and CST', 'HST', 'DST', 'MST', 'MST and PST', 'CST and MST', 'PST', 'CST']) # Keys and values states_tz_dict_items = states_tz_dict.items() print(states_tz_dict_items) # dict_items([('Florida', 'EST and CST'), ('Hawaii', 'HST'), ('Arizona', 'DST'), ('Colorado', 'MST'), ('Idaho', 'MST and PST'), ('Texas', 'CST and MST'), ('Washington', 'PST'), ('Wisconsin', 'CST')])
That’s some iterations we did. But you can also loop through a dictionary with a for loop. That’s what we are going to look at in this tutorial.
What We’ll Cover
How to Iterate through a Dictionary with a for Loop
With the Python for loop, you can loop through dictionary keys, values, or items. You can also loop through the dictionary and put the key:value pair in a list of tuples. We are going to look at them one by one.
How to Iterate through Dictionary Keys with a for Loop
Remember how I got the keys of the dictionary with the keys() method in the first part of this article? You can use the same method in a for loop and assign each of the keys to a variable we can call k :
states_tz_dict = < 'Florida': 'EST and CST', 'Hawaii': 'HST', 'Arizona': 'DST', 'Colorado': 'MST', 'Idaho': 'MST and PST', 'Texas': 'CST and MST', 'Washington': 'PST', 'Wisconsin': 'CST' >for k in states_tz_dict.keys(): print(k) # Result: # Florida # Hawaii # Arizona # Colorado # Idaho # Texas # Washington # Wisconsin
How to Iterate through Dictionary Values with a for Loop
You can use the values() method in a for loop too, and assign the values to a variable you can call v :
states_tz_dict = < 'Florida': 'EST and CST', 'Hawaii': 'HST', 'Arizona': 'DST', 'Colorado': 'MST', 'Idaho': 'MST and PST', 'Texas': 'CST and MST', 'Washington': 'PST', 'Wisconsin': 'CST' >for v in states_tz_dict.values(): print(v) # Result: # EST and CST # HST # DST # MST # MST and PST # CST and MST # PST # CST
How to Iterate through Dictionary Items with a for Loop
The items() method comes in handy in getting the keys and values inside a for loop. This time around, you have to assign two variables instead of one:
states_tz_dict = < 'Florida': 'EST and CST', 'Hawaii': 'HST', 'Arizona': 'DST', 'Colorado': 'MST', 'Idaho': 'MST and PST', 'Texas': 'CST and MST', 'Washington': 'PST', 'Wisconsin': 'CST' >for k, v in states_tz_dict.items(): print(k,"--->", v) # Result: # Florida ---> EST and CST # Hawaii ---> HST # Arizona ---> DST # Colorado ---> MST # Idaho ---> MST and PST # Texas ---> CST and MST # Washington ---> PST # Wisconsin ---> CST
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.
How to Loop through a Dictionary and Convert it to a List of Tuples
To convert a dictionary to a list of tuples in Python, you still have to use the items() method inside a for loop.
But this time around, you have to surround the loop with square brackets. You also have to assign the loop to a separate variable and wrap the variable for both keys and values in brackets:
states_tz_dict = < 'Florida': 'EST and CST', 'Hawaii': 'HST', 'Arizona': 'DST', 'Colorado': 'MST', 'Idaho': 'MST and PST', 'Texas': 'CST and MST', 'Washington': 'PST', 'Wisconsin': 'CST' >list_of_tuples = [(k, v) for k, v in states_tz_dict.items()] print(list_of_tuples) # Result: [('Florida', 'EST and CST'), ('Hawaii', 'HST'), ('Arizona', 'DST'), ('Colorado', 'MST'), ('Idaho', 'MST and PST'), ('Texas', 'CST # and MST'), ('Washington', 'PST'), ('Wisconsin', 'CST')]
Conclusion
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.
If you find this article helpful, don’t hesitate to share it on social media.
Kolade Chris
Web developer and technical writer focusing on frontend technologies. I also dabble in a lot of other technologies.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.