miss t miss t - 5 months ago 17
Python Question

How to sort a dictionary with multiple inputs

I have a dictionary called language list with 2 items: suburb and language.
Each suburb may have more than 1 language.

This is what is in the dictionary "languagelist":
('Edgecumbe', 'Farsi, English'), ('Junction Triangle', 'English, Mandarin'), ('Guildwood', 'Mandarin, English'), ('Greenmeadows', 'English')

The user enters a suburb eg. Edgecumbe
I need to display the languages for that suburb in alphabetical order eg
Languages: English, Farsi

I can only seem to sort the "suburb" item, but cannot sort the languages for that suburb.

How do I display only the languages for the suburb entered by user?

This is the current line of code to display:
print('Languages: ' + languagelist[suburb])

But it is not sorted properly. It is displaying:
Languages: Farsi, English

Any help would be greatly appreciated.


Say you start with

languagelist = dict([('Edgecumbe', 'Farsi, English'), ('Junction Triangle', 'English, Mandarin'), ('Guildwood', 'Mandarin, English'), ('Greenmeadows', 'English')])

Then, for any place, the sorted languages spoken at that place is

', '.join(sorted(languagelist[place].split(', ')))

Of course, it would be much better if you just use a dictionary mapping places into what you want to begin with.