2

Here's my dictionary that maps countries to their populations

countryPop = {'China': 1402112, 'United States': 331449, 'India': 1380004, 'France': 67399, 'Britain': 67216, 'Ukraine': 41588, 'Canada': 38005, 'Russia': 147500, 'Germany': 83100, 'Italy': 59554} 

This is the code to sort by key

 # Display the final dict once the user exits the loop for name, population in sorted(countryPop.items()): # sorted the dict print('Country ', name, ' has population ', population, 'in thousands') 

How could I list all countries in order of the first letter of the country name, sorted by the population? For example, now my output have something like this

... Country Canada has population 38005 in thousands Country China has population 1402112 in thousands ... 

China should go before Canada because it has more population (sort by value)

1

3 Answers 3

1

You need to pass a key parameter to sorted():

for name, population in sorted(countryPop.items(), key=lambda x: (x[0][0], -x[1])): print('Country ', name, ' has population ', population, 'in thousands') 

This will use tuple comparison, comparing items based on the first letter of the key, and then using the value multiplied by -1 as a tiebreaker (since we want to sort by population in descending order).

This outputs:

Country Britain has population 67216 in thousands Country China has population 1402112 in thousands Country Canada has population 38005 in thousands Country France has population 67399 in thousands Country Germany has population 83100 in thousands Country India has population 1380004 in thousands Country Italy has population 59554 in thousands Country Russia has population 147500 in thousands Country United States has population 331449 in thousands Country Ukraine has population 41588 in thousands 
Sign up to request clarification or add additional context in comments.

4 Comments

I tried it but it still gave me the same output as the one I had, could you check the code, please?
I just edited it, try now.
Great it works, thank you! But is it possible to achieve the same output without using lambda?
You could define a function using the def keyword (as you would with any other function) and then pass that function into the key parameter. But in terms of conciseness, I don't think you can beat this. And while you're here, remember to upvote useful answers and accept the one you find most helpful. :)
-1

You will have to convert to a list and sort the list:

allx = list(countryPop.items()) allx.sort( key=lambda x: (x[0][1],x[1]) ) 

Note that the sort key is the first letter of the name, and the population.

2 Comments

If I print àllx, I get something that starts with ('Canada', 38005) instead of ('Britain', 67216). You probably forgot to add dict(allx). Please check the code once!
There's no need for dict(allx), it's just that the key should be x[0][0] instead of x[0][1].
-1

One way I have in mind is using pandas,

df = pd.DataFrame(countryPop.items(), columns = ["key", "value"]) df = df.sort_values(by = list(df.columns)) dict(df.values) 

4 Comments

Thank you, but this might still not resolve my problem, because only keys (the countries) are sorted, but I would like to sort by population after sorted by keys.
I just had a weird realization. Aren't dictionaries already sorted? If you print countryPop you would see that they are already sorted according to the key names.
yes they are already sorted by key names, but I would like it to sort by values after being sorted by keys (if some keys have the same letters, the largest population go first)
I've updated the answers, it would first sort the data by the keys, then the values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.