How To Sort A Dictionary By Value (Python Recipes)

preview_player
Показать описание
In this video I am going to be showing you how you can sort a dictionary by its value in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

Hello everyone! In addition to my longer videos, I've decided to try out a new style of video, which I'm just going to tag as the "Python Recipes" series. It's practically just going to contain Python recipes that help achieve certain tasks. Hope you find these videos helpful!

Indently
Автор

Can I just take a moment to say how much I appreciate this style of video? Short and straight to the point. No 5 minutes of intro/talking, you just get into it. Thanks!

gydo
Автор

Using the minus, ‘-‘ as in key=lambda x: -x[1], will also sort in descending order. This is useful if for example the value is a tuple with (name, age) and you want to sort name ascending but age descending. So use, lambda x: (x[0], -x[1]).

tacobuddy
Автор

A little micro-optimisation recommendation here: using dict(sorted(scores.items(), key = operator.itemgetter(1))) is slightly faster 😁

MilChamp
Автор

I think that orders in dictionaries were introduced just for the purpose of not getting a randomly ordered dict each time you print them after a mutation of the dictionary, so they disallowed any other order-related method that could be put, like insert, sort, pop(i), etc.. The only way of editing the indices is the one shown in the video, but it's just creating an entire new dict, not mutating it.
However there is one index related method for dictionaries which is popitem() that will give you the last item. ( wish they just called it pop() ).

intron
Автор

Massive thanks for your help! I might watch some of your other videos about Python :)

luxmax
Автор

I scrolled through your video list and saw that you did videos on C++ 3 years ago. Maybe you could do a short video showing how you would do this in C++. In the past you might push everything into a vector of pair's and sort by the value, or you might swap the key and value into a new map, but I'm sure there's some functional method that people would use now.

anon_y_mousse
Автор

Highly welcome pal. Do such short videos often to recall stuffs

mohanasundaram
Автор

I appreciate it's mentioned that this is only "true" since 3.7 (before that it was implementation specific)
I personally still think of dicta as unordered data type, even thought it's specified in the documentation that since 3.7 the order is the same as the keys being inserted (like thenew key is added to the end of the keys)

ProfRoxas
Автор

Is there a way to make this sorting in-place? (i.e. without making a copy of the key value pairs)

oldschoolsoldier
Автор

What would be the tome complexity of doing it this way? Just curious.

aniruddhasharma
Автор

pretty sure I've used reversed(sorted(x)), not sorted(x, reversed=True). for TWO DECADES.

DrDeuteron
Автор

no more "How's it going, everyone?" 💔

sebastian_sv
Автор

As @gyd01942 says this style f video and your explanations are excellent. Efficient, precise. Thank you to share like that

davidl