Sorting things out in Python

kwojo

Kevin Wojkovich

Posted on January 18, 2020

Sorting things out in Python

For those that know me personally, I have a weird hobby of vintage computing and have started down a rabbit hole of IBM systems.

Sorting has been a task that IBM provided some solutions to help businesses process data. In the era of punch cards, one may find it necessary to sort the cards that represented program input. For example, a program that processes names may take cards as input with folks' names entered on them. For convenience these cards may need to be sorted after an army of typists cranked out decks of cards. Enter the IBM 80 series card sorting machine. Loudness warning: see the IBM 83 in-action at the Computer History Museum in Mountain View California. Isn't it crazy that a refrigerator-sized machine's single function was sorting? What's more crazy is that the IBM 83 can only sort one character at a time. This means if the sorted cards had to be passed through the machine multiple times to fully sort the deck.

Sorting is a classic problem in computer science. If you've taken an algorithms course, a good chunk of that course can be dedicated to sorting. I love Python because the language wraps up many complex concepts into an easy-to-use experience, so you can go get work done. A reasonable sorting algorithm called Timsort.

Let's look at sorting lists in this post.

class list(object)
 |  list(iterable=(), /)
...
 |  sort(self, /, *, key=None, reverse=False)
 |      Stable sort *IN PLACE*.
 |  

So the list object has a method that affects the state of the variable. Depending on your application, this may or may not be desired.

fruit = ['oranges', 'bananas', 'grapes', 'apples']
fruit.sort()
fruit
>>> fruit
['apples', 'bananas', 'grapes', 'oranges']

Sometimes it's necessary to preserve the original list. What do we do then?

help(sorted)
sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.

    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.

Coolio so we can preserve the original list using the sorted function.

fruit = ['oranges', 'bananas', 'grapes', 'apples']
sorted(fruit)
>>> ['apples', 'bananas', 'grapes', 'oranges']
fruit
>>> ['oranges', 'bananas', 'grapes', 'apples']

This may be a pretty simplistic blog post. As I spend my free time playing tech archaeologist, I find it fascinating the power in higher-level languages, especially accessible languages such as Python.

The takeaways I hope you walk away with:

  • the sort() method on a list is in-place, it changes the variable.
  • the sorted() function returns a new list.
  • The built-in help() function is a helpful reference when you need it.
πŸ’– πŸ’ͺ πŸ™… 🚩
kwojo
Kevin Wojkovich

Posted on January 18, 2020

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

Sign up to receive the latest update from our blog.

Related