How to Sort a Python Dictionary By Value or Key!

How to Sort a Python Dictionary By Value or Key!


Daniel Ostrander

Related Posts

24 thoughts on “How to Sort a Python Dictionary By Value or Key!

  1. Mousus929 says:

    Very helpful, thx man 🙂

  2. Molly Belle says:

    Life saver, thanks so much for making this video

  3. Vitaliy Demchyk says:

    Saved my course grade with this!!

  4. Sep2011100 says:

    Good and precise explanation!

  5. Artist says:

    It produces a list of tuples.

  6. John McLean says:

    is there a way to order the numbers from largest to smallest??

  7. Jessica W says:

    How do you get the sum of all of the values in your dictionary? I used the
    sum(epicDict.values()) function and I got an error.

  8. elbahloul elhoussaine says:

    BRAVOOOOOOOOOOOOOOO

  9. Alexandru Bradatan says:

    Really helpful!! thank you

  10. ADeioHJ says:

    Thank you so much, you probably just saved my coursework in class. Subscribed.

  11. Gyan says:

    u r super cool man if somebody wants to make his carrier in python then ur tutorials are the one which can lead anyone to his destination i have gone though lot of tutorials none is better then ur not even the google tutorials u r rockstar …keep it up….

  12. Upamanyu Ray says:

    Thank you so much for the video. I have a problem

    I have a text file like this :

    1 1 4.59114 0.366832 -9.56424

    2 1 5.24742 -0.870574 -8.40649

    3 2 5.21995 -0.38856 -7.39145

    4 3 6.43785 -1.20272 -8.77666

    5 1 7.53694 -0.0740336 -8.86134

    27 2 7.64588 0.464146 -7.88774

    28 1 7.07087 0.831361 -9.94761

    29 2 6.96326 0.260853 -10.9003

    32 1 5.77529 1.52588 -9.63533

    33 2 5.82876 2.05648 -8.67551

    34 3 5.45996 2.478 -10.7567

    35 2 4.47197 2.81668 -10.674

    36 1 4.26389 -2.0756 -8.48939

    37 2 3.21056 -1.72474 -8.29007

    38 3 4.41887 -2.73285 -9.80971

    39 2 3.67915 -3.47532 -9.87803

    40 2 4.42807 -2.7596 -7.63859

    41 3 3.54752 0.821914 -9.09022

    42 2 4.44697 -0.0904811 -10.564

    31 2 8.07781 2.54128 -9.38259

    Now I want to sort it in 2 steps :

    a) First by using the second column, all the rows having 1, 2 or 3 will be together. SO it will look something like this:

    1 1 4.59114 0.366832 -9.56424

    2 1 5.24742 -0.870574 -8.40649

    32 1 5.77529 1.52588 -9.63533

    5 1 7.53694 -0.0740336 -8.86134 and so on…

    3 2 5.21995 -0.38856 -7.39145

    27 2 7.64588 0.464146 -7.88774

    31 2 8.07781 2.54128 -9.38259 and so on….

    4 3 6.43785 -1.20272 -8.77666
    38 3 4.41887 -2.73285 -9.80971

    37 2 3.21056 -1.72474 -8.29007 and so on….

    After that, I want to sort by using the third column. For example,
    when the value in the 3rd column is between 2 to 5, all those rows will
    be together,third columns having valuers will be together 5-9. . This
    will happen for each of the 3 cases of the second column value being 1, 2
    and 3.

    Can someone please tell me how to achieve this? Sorry, it maybe
    straightforward question but I am a beginner in python currently. Thank
    you so much.

  13. Tan Onay says:

    The video is great but the output of the code is a list of memory addresses. So I cannot turn into type dict again. Is there a way around this? I'm really struggling

  14. Enjoying Life says:

    How about making a dictionary which will order the elements by the order key/value pairs are added to them?

    I know OrderedDict does that but not sure how to use it

  15. Bruno Mello says:

    sorting by keys:

    for key in sorted(epicDict.keys()):
    print(key, ':', epicDict[key])

  16. SAGAR SAURABH says:

    Hi I am glad u posted tis vdo as I was looking for an easy way to do sort . However I stumbled upon one more way .

    import operator
    family= {'mathew': 21, 'raechel': 32, 'mike': 60, 'tom': 75, 'Robbie': 34, 'Maggie': 35}
    print(family)

    #Dictionary in Discending order by value :
    sorted_family= sorted(family.items(), key=operator.itemgetter(1),reverse=True)
    print('Dictionary in ascending order by value : ', sorted_family)
    print()

    #Dictionary in Ascending order by value :
    sorted_family = sorted(family.items(), key=operator.itemgetter(1))
    print('Dictionary in ascending order by value : ',sorted_family)

  17. Martynas Juravičius says:

    If you want dictionary, you can use Comprehensions:
    return {key: value for key, value in sortedByKeyDict}
    and
    return {key: value for key, value in sortedByValueDict}
    in the end of functions. It will return dictionary.

  18. tmcthree says:

    I want to know why Jess is hanging round those children

  19. katerina liu says:

    solve my question regarding the python exercise from the datacamp

  20. Avnish Rai says:

    thanks a lot man i was trying to do it in different way by making key value assigned to third value and then assigning it to orignal value and it didnt work and i wasted my 2 hr in it thanks once again

  21. Panna Pan says:

    Can you plz give me the link of your lambda tutorial? couldnt find it

  22. Liu Hantong says:

    dict.items() returns a list of tuples
    len(dict) returns the size of dict
    “key=lambda t:t[0]” is not necessary
    can also add “reverse = True”

  23. Yury Moskaltsov says:

    Everyone said that this is a life saver and I will repeat. I had 24 line messy code to do the same thing that you did in 2 lines. Thank you!!!

  24. strawberryyogurt0 says:

    Vid: 2013 . My comment: 2020 … For anyone, even though Dictionaries are classified as unordered, and people on stackoverflow says you can’t order them and acts as if question doesn’t make sense, if you look at the key/value pairs (when both ‘numbers’ or ‘letters’ are used for key/value) why would anyone say you can’t order a dictionary? Numbers 0123456789,10,11, etc … Letters abcdefghi, etc, both are in a specific order otherwise we would be counting numbers starting from 5, 6, 3, 4, 2, 9, etc. or the alphabet starting from d, b. F, z, y, etc. The desire to order the dictionary in a way we’ve been taught from childhood is exactly what we want to do. Why do so many people on stackoverflow act as if they don’t know what the user wants to do when he/she ask how to sort the dictionary in a specific order. Even though dict(sorted(Dict_original)) does not put the sorted dict key/value pair in sorted dict format, the question of wanting to know how to sort a dictionary still makes sense.

Leave a Reply

Your email address will not be published. Required fields are marked *