How to iterate over map keys and values in Python

3 Answers

0 votes
my_dict = {'a': 1, 'b': 2, 'c': 3}

for key, value in my_dict.items():
    print(f"Key: {key}, Value: {value}")


'''
run:

Key: a, Value: 1
Key: b, Value: 2
Key: c, Value: 3
   
'''

 



answered Apr 19 by avibootz
0 votes
my_dict = {'a': 1, 'b': 2, 'c': 3}

# Iterating over keys
for key in my_dict.keys():
    print(f"Key: {key}")

# Iterating over values
for value in my_dict.values():
    print(f"Value: {value}")


'''
run:

Key: a
Key: b
Key: c
Value: 1
Value: 2
Value: 3
   
'''

 



answered Apr 19 by avibootz
0 votes
my_dict = {'a': 1, 'b': 2, 'c': 3}

for key in my_dict:
    print(f"Key: {key}, Value: {my_dict[key]}")


'''
run:

Key: a, Value: 1
Key: b, Value: 2
Key: c, Value: 3

   
'''

 



answered Apr 19 by avibootz
...