Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to sort a list alphabetically in Python

Getting Started with Sorting

When you’re learning programming, you’d come across an instance where you have a list of items that you need to sort in some specific order. In Python, one common task is sorting a list alphabetically. This task might seem simple, but it can get a bit tricky if you're still new to programming. Not to worry, we're going to break it down to make it as easy as possible.

Understanding the Basics

Before we dive into sorting a list alphabetically, let's make sure we understand what a list is. In Python, a list is a collection of items, each item could be a string, a number, a boolean, or even another list. Think of it like a shopping list where you have different items you want to buy.

The Sort() Method

Python provides a built-in method called sort() that you can use to sort the items in a list. To sort a list alphabetically, you'll need to call this method on your list. Here's a simple example:

fruits = ['banana', 'apple', 'kiwi', 'cherry']

When you run this code, Python will print out: ['apple', 'banana', 'cherry', 'kiwi']The items in the fruits list have been sorted in alphabetical order.

Sorting in Reverse Order

Sometimes, you might want to sort the list in reverse order. Python has got you covered. The sort() method accepts an optional parameter called reverse. If you set reverse to True, Python will sort the list in reverse order. Here's how:

fruits = ['banana', 'apple', 'kiwi', 'cherry']

This will print: ['kiwi', 'cherry', 'banana', 'apple']Now, the fruits list is sorted in reverse alphabetical order.

Sorting a List of Strings with Mixed Case

Things can get a bit tricky when your list contains strings with mixed case. By default, the sort() method is case sensitive, which means it will place uppercase letters before lowercase ones. So if we have a list like this: ['Banana', 'apple', 'Kiwi', 'Cherry'], sorting it would give this result: ['Banana', 'Cherry', 'Kiwi', 'apple'].

To sort a list of strings with mixed case in Python, you can use the key parameter of the sort() method to specify a function that will be called on each item before it's compared for sorting. Here's an example:

fruits = ['Banana', 'apple', 'Kiwi', 'Cherry']

This will print: ['apple', 'Banana', 'Cherry', 'Kiwi']The str.lower function is called on each item, converting it to lowercase before it's compared for sorting. This ensures the list is sorted alphabetically without considering the case.

Conclusion - Embrace the Magic of Python

Sorting a list alphabetically in Python may seem like a magic trick at first, but once you understand the mechanics behind it, it's just another tool in your Python toolkit. It's like learning to ride a bike. At first, keeping your balance seems like an impossible task. But once you get the hang of it, it becomes second nature.

Python's built-in sort() method is a powerful function that can help you manipulate and organize your data effectively. So next time you find yourself with a jumbled list of items, just remember the sort() method, and you'll have them in order in no time.

Remember, the best way to learn programming is by doing. So, try to create your own lists and use the sort() method to sort them. Happy coding!