Oct-01-2019, 01:21 PM
There are three ways to loop over a dictionary. Say we have this dictionary:
knights = {'Lancelot': 'Blue', 'Robin': "I don't know that", 'Galahad': 'Blue, no green', 'Arthur': 'What do you mean?'}If we do a plain loop over the dictionary (
for name in knights:
) we get the keys: Lancelot; Robin; Galahad; and Arthur. We can also loop over the values (for answer in knights.values():
) and get Blue; I don't know that; Blue, no green; and What do you mean? We can also loop over both with items (for name, answer in knights.values():
) and get the pairs (Lancelot, Blue); (Robin, I don't know that); and so on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures