How to sort a dictionary with multiple inputs
<p>I have a dictionary called language list with 2 items: suburb and language.
Each suburb may have more than 1 language.</p>

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

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

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

<p>How do I display only the languages for the suburb entered by user?</p>

<p>This is the current line of code to display:</p>

<pre><code>print('Languages: ' + languagelist[suburb])
</code></pre>

<p>But it is not sorted properly. It is displaying:
Languages: Farsi, English</p>

<p>A snip of my current code is: </p>

<pre><code>suburbs={} languagelist={} b=[] suburb=input('Suburb: ') 
for line in open('census.txt'): line=line.split(',') 
if line[3] not in suburbs: suburbs[line[3]] = line[3] a=line[4]a=a[:-1] languagelist[line[3]]= a elif line[3] 
in suburbs: suburbs[line[3]] = suburbs[line[3]] a=line[4] a=a[:-1]
if a not in languagelist[line[3]]: a=line[4] a=a[:-1] languagelist[line[3]]= languagelist[line[3]] + ', ' + a b=languagelist[line[3]] b.split(', ') languagelist[line[3]]=[] languagelist[line[3]]=b 
else: continue while suburb: if suburb not in suburbs: 
print('No data found for ' + suburb + '.')       
else:print('Languages:'+str(sorted(languagelist.suburb)))suburb=input('Suburb: ') 
</code></pre>

<p>Any help would be greatly appreciated.</p>

<p>Say you start with</p>

<pre><code>languagelist = dict([('Edgecumbe', 'Farsi, English'), ('Junction Triangle', 'English, Mandarin'), ('Guildwood', 'Mandarin, English'), ('Greenmeadows', 'English')])
</code></pre>

<p>Then, for any <code>place</code>, the sorted languages spoken at that place is</p>

<pre><code>', '.join(sorted(languagelist[place].split(', ')))
</code></pre>

<hr>

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

python sorting dictionary