Python 3. 3+ has a str.casefold method specifically designed for case insensitive matching:
sorted_list = sorted(unsorted_list, key=str.casefold)
In Python 2, use lower() :
sorted_list = sorted(unsorted_list, key=lambda s: s.lower())
It works for both regular and unicode strings, as they both have a lower method.
In Python 2, this works for a mixture of regular and unicode strings, as the values โโof the two types can be compared with each other. However, Python 3 does not work this way: you cannot compare a byte string and a string in Unicode, so in Python 3 you have to perform reasonable actions and sort lists of only one type of string.
>>> lst = ['Aden', u'abe1'] >>> sorted(lst) ['Aden', u'abe1'] >>> sorted(lst, key=lambda s: s.lower()) [u'abe1', 'Aden']
user25148 Apr 22 2018-12-22T00: 00Z
source share