Sorting items in a list or array is a common task for any programmer. In the Python world, this can be accomplished using the built-in sort() function or the built-in function sorted().
While these functions are great when it comes to sorting numbers or strings alphabetically, when it comes to sorting strings lexicographically, things aren’t as straightforward. However, Python provides us with a way to sort strings lexicographically very easily.
In this tutorial, we’ll walk you through how to sort a list of strings lexicographically in Python.
Step 1: Understand Lexicographical Order
The term “lexicographical order” comes from “lexicon”, which is like a dictionary. In simpler terms, lexicographic or dictionary order means that the words are arranged as they would appear in a dictionary, taking into account not just the first character, but the entire word.
Step 2: Get to Know the sort() Function
In Python, the list function sort() is used to arrange the elements in ascending order by default. If you want to arrange in descending order, you can simply pass the keyword reverse =True.
1 2 3 |
list_sample = ['apple', 'mango', 'banana', 'kiwi'] list_sample.sort() print(list_sample) |
Step 3: Using the sort() Function for Lexicographical Order
It’s a common misunderstanding that the sort() function will not provide a lexicographic sort, but this is actually not true. The Python sort() function can correctly sort a list of strings lexicographically without any additional parameters. Here is how you can do it:
1 2 3 |
list_sample = ['apple', 'Mango', 'banana', 'Kiwi'] list_sample.sort() print(list_sample) |
Step 4: Understanding the Output
After executing the above script, you will notice that it doesn’t sort according to the dictionary because it uses ASCII values for sorting which puts letters ‘M’ and ‘K’ before ‘a’ and ‘b’. This is due to the ASCII value for ‘M’ (77), and ‘K’ (75) being lower than for ‘a’ (97), and ‘b’ (98).
Step 5: Case Insensitive Sorting
To sort our list in dictionary order where case does not matter we can use a parameter key function str.lower to the sort function.
1 2 3 |
list_sample = ['apple', 'Mango', 'banana', 'Kiwi'] list_sample.sort(key=str.lower) print(list_sample) |
The Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Step 2 list_sample = ['apple', 'mango', 'banana', 'kiwi'] list_sample.sort() print(list_sample) # Output: ['apple', 'banana', 'kiwi', 'mango'] # Step 3 list_sample = ['apple', 'Mango', 'banana', 'Kiwi'] list_sample.sort() print(list_sample) # Output: ['Kiwi', 'Mango', 'apple', 'banana'] # Step 5 list_sample = ['apple', 'Mango', 'banana', 'Kiwi'] list_sample.sort(key=str.lower) print(list_sample) # Output: ['apple', 'banana', 'Kiwi', 'Mango'] |
['apple', 'banana', 'kiwi', 'mango'] ['Kiwi', 'Mango', 'apple', 'banana'] ['apple', 'banana', 'Kiwi', 'Mango']
Conclusion
In this tutorial, we have learned how the built-in sort function in Python works. We covered the basics of sorting and how to sort strings lexographically without considering the case.
We also highlighted how the ASCII values of characters influence sorting. This is important to understand the underlying logic of sorting in Python and other similar programming languages. Happy coding!