Descargar el Código: patreon.com/pythonmaraton
Join Patreon: patreon.com/pythonmaraton
^Downloadable code & more! [ Ссылка ]
Sort dictionary by keys
In this video we will learn how to sort a python dictionary by its keys.
First let’s open up python and let’s create a string of the alphabet.
Next let’s use that string to create a dictionary. We will create it in reverse order, so that we can then sort it by keys.
dictionary = {string[::-1][i]:27-(i+1) for i in range(26)}
Now that we have our dictionary let’s sort it by the keys.
We import collections. we will create x which will be the sorted by keys version of our dictionary.
x = collections.OrderedDict(sorted(dictionary.items()))
So now lets compare. Here is x, which we type dict to convert it back to a normal dictionary, and here is the original dictionary.
You can see x now is alphabetic. a first then b and so on.
So there you have it, that is how you can sort a python dictionary by keys.
Please subscribe for more python content :D
This is a Python anaconda tutorial for help with coding, programming, or computer science. These are short python videos dedicated to troubleshooting python problems and learning Python syntax. For more videos see Python Help playlist by Rylan Fowers.
✅Subscribe: [ Ссылка ]...
📺Channel: [ Ссылка ]...
▶️Watch Latest Python Content: [ Ссылка ]...
▶️Watch Latest Other Content: [ Ссылка ]...
🎵Theme Music: [ Ссылка ]...
#PythonMarathon #LearnPython #PythonTutorial
Python Dictionary Sort by Key
Теги
python tutorial dictionarysorting dictionary by keysdictionary pythonsort python dictionarysort python dictionary by valuesort python dictionary by keysort dictionary by key pythonpython sorted dictionaryhow to sort python dictionary by keyhow to sort a python dictionarypython sort keypython dictionary tutorialpython numpy dictionarypython tutorial for beginners with examplespython dictionary tutorial from scratch