One can use a for
loop to iterate over the key/value pairs in a map object. For example,
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.items():
print(key, value)
This will print:
a 1
b 2
c 3
Alternatively, one can also use a while
loop and the iter()
function to get an iterator for the map object:
my_dict = {'a': 1, 'b': 2, 'c': 3}
it = iter(my_dict.items())
while True:
try:
key, value = next(it)
print(key, value)
except StopIteration:
break
This will also print:
a 1
b 2
c 3
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-09-26 11:00:00 +0000
Seen: 11 times
Last updated: Oct 07 '22
What is the process to detect and count common elements in R?
How can you space out HTML elements using CSS exclusively?
Can an Array of Elements be made in Android?
What is the method to show a line break <br> between <div> elements in Laravel?
When does the ForEach loop come to an end? Does it happen when a null value is returned?