Sort Elements in Python

????The sort methodUnlike the sorted function, the sort method sorts the list in-place returning None.

The sort method has the following syntax with two optional parameters as well: key and reverse.


sort(key=…, reverse=…)With the following examples, we can understand better how both the sorted function and the sort method sort elements of a list.

We can sort elements of a list basically because list are ordered containers.

Additionally, we can sort elements in-place because lists are mutable; we can change their elements.

Additional parameters: reverse and keyBoth the sorted function and the sort method sort the elements in ascending order, unless we indicate otherwise, providing the parameter reverse=True.

Another important parameter is key.

We can sort a list based on the value returned by the function provided in the key parameter.

We apply the function to each element of the list and we sort the elements based on the value returned by this function.

The key parameter can contain built-in, anonymous, or normal functions as well as methods.

In the following examples, we use the key parameter to:Sort a list of strings by its length.


Sort a list of strings by the number of vowels.


Sort a nested list by the third value.

Sort elements in a tupleA tuple is a data type for immutable ordered sequences of elements.

To sort elements of a tuple, we can use the sorted function, providing the tuple as the first argument.

This function returns a sorted list from the given iterable, and we can easily convert this list into a tuple using the built-in function tuple.

We can use the two optional parameters (key and reverse) as well.

As tuples are immutable, we can not use the sort method as we previously did with lists (An AttributeError exception is raised), since this method modifies the iterable in-place.

Sort elements in a setA set is a data type for mutable unordered collections of unique elements.

We can sort the elements of a set (sorted list), in the same way we previously sorted the elements of a list or a tuple, using the sorted function.

It has not sense to convert the returned sorted list into a set, as set are unordered containers (It does not matter the position of the elements).

As sets are unordered, we can not use the sort method (An AttributeError is raised), since we can not order in-place a container that is unordered.

⚡️Sort elements in a dictionaryA dictionary is a data type for mutable objects that store mappings of unique keys to values.

We can use the sorted function with dictionaries as we previously did with other iterables, obtaining a sorted list of dictionary keys.

Furthermore, we can obtain a sorted list of dictionary keys by value, applying the get method in the key parameter as follows:In both previous cases, we obtain a list rather than a dictionary.

To obtain a sorted dictionary, we can use the sorted function in combination with dictionary comprehensions.

In the following examples, the dictionary is sorted according to its keys and values.

We can also apply the reverse parameter to descending order sorting.

The collections containerThe collections module provides alternative container datatypes to built-in python containers: list, tuple, set, and dictionary.

These containers can also be sorted using the sorted function.

Let’s see an example!OrderedDict is a container provided by the collection module, consisting of dict that remembers the order in which its contents are added.

It basically works as a dictionary :), accepting dictionary methods and functions.

We can use the sorted function with ordered dictionaries as we previously did with normal ones as follows:The collection module provides alternative containers that can make our programming life much easier ????.

See the documentation for more information about how these containers can be used ????8.


collections – High-performance container datatypes – Python 2.


16 documentationA is a subclass for counting hashable objects.

It is an unordered collection where elements are stored as dictionary…docs.


orgOverviewContainers are objects that can contain other objects such as lists, sets, tuples or dictionaries.

Mutability and order are two key aspects to understand how a container can be sorted.

The sorted function can be applied to iterables to sort their elements.

This function returns always a list.

Lists accept the sort method as well.

This method modifies the list in-place, returning None.

The containers provided by the collection module can also be sorted using the sorted function.

Thanks for reading ????.????.. More details

Leave a Reply