Hy, I'm a begginer, I want to know how to sort a dictionary after values, and to print another dictionary, which is sorted. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Hy, I'm a begginer, I want to know how to sort a dictionary after values, and to print another dictionary, which is sorted.

How to sort a dictionary in Python

27th Dec 2019, 12:35 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
19 Answers
+ 4
You should make that a whole new question. And write explicitly, what you want to accomplish.
27th Dec 2019, 2:09 PM
HonFu
HonFu - avatar
+ 4
by key or by value? By key: print(sorted(that_dict.items(), reverse=True))
27th Dec 2019, 12:40 PM
HonFu
HonFu - avatar
+ 4
You can, as I said, not output as a dict, but this does come close. Run and check: d = {'a': 3, 'b': 2, 'c':4} print(sorted(d.items(), key=lambda x: x[1], reverse=True))
27th Dec 2019, 12:59 PM
HonFu
HonFu - avatar
+ 4
You should post it separately from this thread - as a new question.
27th Dec 2019, 4:13 PM
HonFu
HonFu - avatar
+ 3
A dictionary can't be sorted, you can only create a sorted list from a dictionary. How exactly do you want to sort it?
27th Dec 2019, 12:37 PM
HonFu
HonFu - avatar
+ 3
Ha, you found it already. :)
27th Dec 2019, 1:01 PM
HonFu
HonFu - avatar
+ 2
Thank you very much. It works! I wrote there print (sorted(dic. items(), key=lambda p:p[1] ,reverse=True)) and was very good!
27th Dec 2019, 12:56 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
+ 2
I understand now. I didnt know. Thanks.
27th Dec 2019, 5:01 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
+ 2
How about C#
28th Dec 2019, 12:40 AM
Rrrrrr
Rrrrrr - avatar
+ 2
Mini Why have you posted this tinder link. It is totally inappropriate. Gotta inform a Mod now!
29th Dec 2019, 11:44 AM
DarkRanger
DarkRanger - avatar
+ 1
And I have another question. It is about "deleting"(removing) vowels from a string, without use any additional string, or auxiliary string.
27th Dec 2019, 1:46 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
Descending
27th Dec 2019, 12:38 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
For egz: l="I have an apple ." I want a dictionary which have the letters and how offten they appear there, in string. For ex: dict={'I':1, 'h' :1,'a':3, etc} But I have to sort it descending after the values (how offten they appear) and return a dictionary too, with this properties.
27th Dec 2019, 12:44 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
Like: print(------) ---> {'a':3,'e':2,'i':1 } Something like this.
27th Dec 2019, 12:47 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
And I have another question. Actually, I have a lot of questions😂😂😂. It îs about "deleting"(removing) vowels from a string, without use any additional string, or auxiliar string.
27th Dec 2019, 1:12 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
I'm writting like this, with some mistakes because I'm romanian. 😂😂
27th Dec 2019, 1:13 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
In the collections module you can find an object called OrderedDict, which is, as the name says, an ordered dictionary. Maybe that is what you're looking for. If so, you don't need to do any extra work, because OrderedDict does everything for you. Hope that's helpful! Good luck!
27th Dec 2019, 1:42 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar
0
Thanks.
27th Dec 2019, 1:45 PM
Frontend Dev Junior
Frontend Dev Junior - avatar
0
Python's strings are immutable objects which makes them faster, so you cannot perform in-place solutions. At least you will need aditional string to remove those vowels. Immutable = unchangeable
29th Dec 2019, 7:57 AM
DarkRanger
DarkRanger - avatar