Loading lessons...
Loop Through Dictionaries
Loop Through Dictionaries
A plain for loop gives you the keys; other methods give you more.
Loop keys
thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964}
for x in thisdict:
print(x) # brand, model, year
Loop keys explicitly
for x in thisdict.keys():
print(x)
Loop values
for x in thisdict:
print(thisdict[x])
Or use values():
for x in thisdict.values():
print(x)
Loop both key and value
for x, y in thisdict.items():
print(x, y)
TL;DR
for k in diterates keys.for k in d.keys()is explicit.for v in d.values()iterates values.for k, v in d.items()iterates both.