Question: Python Coding Question Please write a function called convertDictionaryToList that accepts a single argument: a dictionary variable. Your function should build and return a nested
Python Coding Question
Please write a function called convertDictionaryToList that accepts a single argument: a dictionary variable. Your function should build and return a nested list variable that contains the contents of the dictionary, sorted by key.
For example, if the input dictionary were the following:
{"cat": 3, "badger": 5, "parrot": 1} Your return value would be:
[["badger", 5], ["cat", 3], ["parrot", 1]]
Test code (to be executed after your solution):
animalWeights = { "dog": 45.2, "cat": 22.9, "monkey": 33.3, "turkey": 28.8} treeHeights = { "cactus": 15.4, "grass": 2.1, "sunflower": 18.4, "pine": 118.7} print(convertDictionaryToList(animalWeights)) print(convertDictionaryToList(treeHeights))
Expected Result:
[['cat', 22.9], ['dog', 45.2], ['monkey', 33.3], ['turkey', 28.8]] [['cactus', 15.4], ['grass', 2.1], ['pine', 118.7], ['sunflower', 18.4]]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
