Python Sorting

musale

Musale Martin

Posted on August 26, 2019

Python Sorting

Introduction

Sorting is a common functionality in day-to-day programming. If you use Python, you are probably familiar with the two common ways you can sort your list values. The sorted() function is a common way to sort any iterable while list.sort() is built into the list. The main difference between the two functions is that list.sort() modifies the list in-place while sorted() creates a new list of sorted items.

Basic sorting

>>> sorted([4, 1, 3, 5, 2, 5, 8])
[1, 2, 3, 4, 5, 5, 8]
>>> l = [8, 24, 63, 12, 5, 23, 2, 6, 7, 8]
>>> l.sort()
>>> l
[2, 5, 6, 7, 8, 8, 12, 23, 24, 63]
Enter fullscreen mode Exit fullscreen mode

Tip 1: Reverse ordering

Both methods allow an optional reverse key which is a boolean value. You can use this to change the order of the sort from ascending to descending.

>>> sorted([4, 1, 3, 5, 2, 5, 8], reverse=True)
[8, 5, 5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

Tip 2: Key function

The 2 list methods have a key function which allows you to pass in a function that is run before sorting each value.

For instance, if I want to order a string using the upper case I would do:

>>> sorted("For instance, if I want to order a".split(), key=str.upper)
['a', 'For', 'I', 'if', 'instance,', 'order', 'to', 'want']
Enter fullscreen mode Exit fullscreen mode

For some cases, you might have a list of complex objects which you want to sort with their own index. You can pass a function referencing the index:

Say you have a list of handbag objects with Name, Size and Price properties, you can sort them with the Price like:

>>> handbags = [("Gucci", "L", 123), ("Vuitton", "M", 251), ("Jacobs", "S", 207)]
>>> handbags.sort(key=lambda bag: bag[2] )
>>> handbags
[('Gucci', 'L', 123), ('Jacobs', 'S', 207), ('Vuitton', 'M', 251)]
Enter fullscreen mode Exit fullscreen mode

Tip 3: Multiple Level Sorting

Suppose you want to sort the bags according to their Price and Size, you can get that done by using Python's operator module which provides itemgetter and attrgetter.

>>> from operator import itemgetter, attrgetter
>>> sorted(handbags, key=itemgetter(1,2))
[('Gucci', 'L', 123), ('Vuitton', 'M', 251), ('Jacobs', 'S', 207)]
Enter fullscreen mode Exit fullscreen mode

Or make it a list of objects with named attributes and use the names to sort:

>>> class Bag:
...    def __init__(self, name, size, price):
...      self.name = name
...      self.price = price
...      self.size = size
...    def __repr__(self):
...      return repr((self.name, self.size, self.price))
...
>>> bags = [Bag("Gucci", "L", 123), Bag("Vuitton", "M", 251), Bag("Jacobs", "S", 207)]
>>> bags.sort(key=attrgetter("price", "size"))
>>> bags
[('Gucci', 'L', 123), ('Jacobs', 'S', 207), ('Vuitton', 'M', 251)]
Enter fullscreen mode Exit fullscreen mode

Conclusion

Sorting list items in Python is pretty easy and these tips can be used to achieve even much more with very little coding complexity. If you're worried about the stability of the sorting, Python sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved.
Python uses Timsort algorithm that does multiple sorts efficiently because it can take advantage of any ordering already present in a dataset. You can read more about sorting in Python here

💖 💪 🙅 🚩
musale
Musale Martin

Posted on August 26, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Python Sorting
python Python Sorting

August 26, 2019