Return to site

Python sort dictionary

broken image
broken image

> sorted(month, key=numbermap._getitem_)Īs we can see, it still returned a sorted list of its first argument (month). Unlike Python lists or tuples, the key and value pairs in dict objects are not in any particular order, which means we can have a dict like this: The values can be objects of any type (dictionaries can even be nested with other dictionaries) and the keys can be any object so long as it's hashable, meaning basically that it is immutable (so strings are not the only valid keys, but mutable objects like lists can never be used as keys). The dict (dictionary) class object in Python is a very versatile and useful container type, able to store a collection of values and retrieve them via keys. Note: If you want to sort a list, tuple or object in Python, checkout this article: How to Sort a List or Tuple in Python Last Updated: Wednesday 29 th December 2021

broken image