Skip to main content
Better title and question clarified
Source Link
pho
  • 25.7k
  • 8
  • 48
  • 75

How to sort firstly by the first letter of the key and then by the value in a dictionary

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, but I don't know how could I then sort by value if the keys have the same first letter

 elif name == '0': # break the loop if user enters 0  # 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')  break 

ForHow 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 ... 
... 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)

How to sort firstly by the key then by the value in a dictionary

Here's my dictionary

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, but I don't know how could I then sort by value if the keys have the same first letter

 elif name == '0': # break the loop if user enters 0  # 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')  break 

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)

How to sort by the first letter of the key and then by the value in a dictionary

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)

Source Link

How to sort firstly by the key then by the value in a dictionary

Here's my dictionary

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, but I don't know how could I then sort by value if the keys have the same first letter

 elif name == '0': # break the loop if user enters 0 # 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') break 

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)