June 2017
Beginner
352 pages
8h 39m
English
As we have seen in an earlier chapter, dictionaries are iterable and so can be used with for-loops. The dictionary yields only the key on each iteration, and it's up to us to retrieve the corresponding value by lookup using the square-brackets operator:
>>> colors = dict(aquamarine='#7FFFD4', burlywood='#DEB887',... chartreuse='#7FFF00', cornflower='#6495ED',... firebrick='#B22222', honeydew='#F0FFF0',... maroon='#B03060', sienna='#A0522D')>>> for key in colors:... print("{key} => {value}".format(key=key, value=colors[key]))...firebrick => #B22222maroon => #B03060aquamarine => #7FFFD4burlywood => #DEB887honeydew => #F0FFF0sienna => #A0522Dchartreuse => #7FFF00cornflower => #6495ED
Notice that the keys are returned ...
Read now
Unlock full access