Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Exercise 16.1

Loop through the dictionary mushrooms printing each scientific name.

mushrooms = { 'Fistulina hepatica':'Beefsteak', 'Laetiporus sulphureus':'Chicken-of-the-Woods', 'Flammulina velutipes':'Enoki', 'Hericium erinaceus':'Lion’s Mane', 'Pholiota nameko':'Nameko', 'Pleurotus ostreatus':'Pearl oyster', 'Ganoderma lucidium':'Reishi', 'Lentinus edodes':'Shiitake'}
for mushroom in mushrooms: print(mushroom)

Exercise 16.2

Loop through mushrooms printing both the scientific and common names.

for sci_name, common_name in mushrooms.items(): print(f'{sci_name}\t{common_name}')

Exercise 16.3

Loop through mushrooms printing the scientific name in alphabetical order.

for mushroom in sorted(mushrooms): print(mushroom)

Exercise 16.4

Loop through mushrooms with the scientific name in reverse alphabetical order.

for sci_name, common_name in sorted(mushrooms.items(), key=lambda kv: kv[1]): print(f'{sci_name}\t{common_name}')