List of tuples | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

List of tuples

This is a program for voting: In the first line, the total number of votes and in the next lines the names of the countries are received as votes Finally, it will print the names of the countries and the number of votes of that country in alphabetical order and in several lines: Input: 5 Italy India India Brazil Spain Output: Brazil 1 India 2 Italy 1 Spain 1 But my output is not like this... What should i do? https://code.sololearn.com/cohymTPyodld/?ref=app

31st Oct 2022, 4:16 PM
Amateur
4 Answers
+ 9
Amateur , this is your code, that is simplified for sorting and output. see also my comments. https://code.sololearn.com/cB1Vi2z7E6ro/?ref=app
1st Nov 2022, 3:44 PM
Lothar
Lothar - avatar
+ 5
Use countries.keys() instead of countries.items() And then print dictionary according to sorted list values..
31st Oct 2022, 5:02 PM
Jayakrishna 🇮🇳
+ 5
You can also use comprehension to build a new, sorted dictionary based on the tuples in that sorted list ... sorted_countries = { e[ 0 ] : e[ 1 ] for e in sorted( countries.items() ) } print( sorted_countries )
31st Oct 2022, 5:50 PM
Ipang
+ 2
Jayakrishna🇮🇳 Ipang Thank u 🌷🌷
31st Oct 2022, 7:19 PM
Amateur