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.
Related Categories
Related Articles
- Unpack and pass list, tuple, dict to function arguments in Python
- Merge multiple dictionaries and add items to a dictionary in Python
- Swap dictionary keys and values in Python
- Get value from dictionary by key with get() in Python
- Get maximum/minimum values and keys in Python dictionaries
- Add an item if the key does not exist in dict with setdefault in Python
- Create a dictionary in Python (<>, dict(), dict comprehensions)
- Sort a list of dictionaries by the value of the specific key in Python
- Change dictionary key in Python
- Get key from value in dictionary in Python
- Remove an item from a dictionary in Python (clear, pop, popitem, del)
- Pretty-print with pprint in Python
- Check if key/value exists in dictionary in Python
- Set operations on multiple dictionary keys in Python
- Extract specific key values from a list of dictionaries in Python