jazx
(Jason Leman)
March 6, 2023, 3:55am
1
Hello, I need help to use the keys method for dictionaries, the sort method for lists, and a for loop to print the keys and corresponding values in the alphabetical order of the keys. However, I am stuck on line 3 of the following example:
hospital_debt = {“Peter”: “$5000”, “Francine”: “$2000”, “David”: “$10,000”}
patients = list(hospital_debt.keys())
patients.sort()
Could someone please point out a way to use the for loop to arrive at the following result?
David, $10,000 Francine, $2000 Peter, $5000
rob42
(Rob)
March 6, 2023, 8:02am
2
Okay. You now have a list
object: ['David', 'Francine', 'Peter']
which you can loop over.
If you have a read of this and this , you should be able to create the loop and access the dictionary.
It’s then simply a matter of formatting a print()
function for the output.
Please post your code formatted. Before posting, your code should be fenced off like this:
```
code block here
```
1 Like
By the way you do not need to call the keys function in cases like this.
parents = list(hospital_debt)
1 Like
jazx
(Jason Leman)
March 6, 2023, 2:40pm
4
Thank you for the pointer!