Sorting in python


Sorting in python

Sorting means arranging a large number of values in a specific order such as ascending and descending, alphabetical, distance, etc. Python provides built-in functions that offer powerful features to do basic sorting or customize ordering at a smooth level. Python has a built-in sort() list method that modifies it in-place and a sorted() built-in function that builds a new sorted list from an iterable.

list.sort()

The easiest way to sort elements of a list is by using the built-in sort() function. It modifies the values of the original list in-place and returns None. The sort() method must be called on a list; it does not work on any other iterable. When it is called, it traverses the list elements in a loop and rearranges them. As the sort() method does not need to create a new list, so has a little faster and efficient execution as the elements to sort is already in a list. The syntax of the sort() method is:

list.sort(key = …, reverse = …)

If the sort() gets called without any parameters, it sorts the list in the ascending order by default. However, it can be customized through optional keyword arguments.

    • reverse (optional):- If “reverse = true”, then the list gets sorted in the descending order. By default its value is False.
    • key (optional):- key parameter defines the base criteria for sort. Its value should be a function that transforms each element before comparison.

sorted()

The sorted() built-in method performs the same as sort().  The difference is that it takes an iterable and returns a sorted list containing the items from the iterable, without modifying the original iterable. It provides the functionality to sort the objects of different data types. It performs the same as sort(). The difference is that it takes iterables The sorted function syntax is:

sorted(iterable, key = None, reverse = False)

Python sorted() uses the Timsort algorithm which is a hybrid sorting algorithm, derived from merge sort and insertion sort and has O(n log n) time complexity. If sorted() is called with no additional arguments then elements of iterable are sorted in ascending order.  Just list sort(), sorted() also has key and reverse optional keyword arguments that produce the same robust functionality.

    • iterable (required):-  sequence (list, tuple, string) or collection (dictionary, set) or any other iterator that needs to be sorted.
    • reverse (optional):- If “reverse = true”, then the iterable gets sorted in the descending order. By default its value is False.
    • key (optional):- key parameter defines the base criteria for sort. Its value should be a function that transforms each element before comparison.

Sorting List

Apart from the list, in python, there are also other iterables like tuple, string, set, dictionary, etc. According to the definition, sorted() method always returns a new list even though the input type is any iterable. Thus to match the input type, the returned object can be cast to input type iterable.

Sorting Tuple

Sorting String

Each element means each letter of the sentence of the string including spaces will get rearrange using sorted() method instead of each word.

For rearranging each word instead of each character, split() built-in string method can be used, converting sentence into a list of strings.

Sorting Set

As a set is an unordered collection, casting resulting list back to a set might change the sorted order.

Sorting Dictionary

Depending on the data passed on to the sorted() method, dictionaries can be sorted in two different ways in Python. The dictionaries are by default rearranged based on their “key” field by using the sorted() method.

 

Add Comment

%d bloggers like this: