To loop the elements of the Python dictionary object dict with a for statement, use the methods keys (), values (), items () of the dictionary object dict.
test = {'key1': 1, 'key2': 2, 'key3': 3}
** keys (): for loop processing for each element key **
for A in test.keys():
print(A)
# key1
# key2
# key3
** values (): for loop processing for each element value **
for B in test.values():
print(B)
# 1
# 2
# 3
** items (): for loop processing for key and value of each element **
for C, D in test.items():
print(C, D)
# key1 1
# key2 2
# key3 3
Recommended Posts