How do I print the key-value pairs of a dictionary in python
You can use the items()
method to print the key-value pairs of a dictionary in Python. Here's an example code snippet:
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.items():
print(key, value)
This will output:
a 1 b 2 c 3
You can also use the for
loop to iterate over the keys of the dictionary and then use the key to access the corresponding value. Here's an example code snippet:
This will also give the same output as the previous example
a 1 b 2 c 3