Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
The book is {"meera": "99887", "asha": "98765", "ravi": "91234", "kabir": "90000"}. In what order does for name in sorted(contacts) show the names?
sorted(dictionary) sorts the keys.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
sorted(contacts) sorts the keys, in alphabetical order: asha, kabir, meera, ravi.
With name.title() they show as Asha, Kabir, Meera, Ravi, each with its number.
The dictionary itself keeps the order in which the contacts were added. Only sorted changes the order of the list.
Log in to join the discussion.
Loading…