To ignore the capitalization, you can simply call the x.lower() method on each element x before sorting the list. Intuitively, you would expect the string 'ab' to occur before 'Ac', right? This way, it can lead to strange sortings like this: lst = The problem with the default list.sort() or sorted(list) method is that they consider capitalization.
Python List Sort Alphabetically Case Insensitive
If the first character is the same, it compares the second character in each string, and so on. In Python, comparing strings to determine the order works by comparing the Unicode of the first characters in both the strings.
By default, it sorts the list in ascending order but you can also sort the list in descending order by passing reverse=True. It returns a new list with the elements in sorted order and doesn’t modify the original list. The following is its syntax: # sort a list You can also use this function to sort other iterables like tuple, string, etc. You can use the Python built-in sorted() function to sort a list.
#Python sort list how to#
In this tutorial, we will look at how to sort a list of strings in alphabetical order in Python with the help of some examples.