case-insensitive list sorting, without lowercasing the result?

84,484

Solution 1

In Python 3.3+ there is the str.casefold method that's specifically designed for caseless 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 normal and unicode strings, since they both have a lower method.

In Python 2 it works for a mix of normal and unicode strings, since values of the two types can be compared with each other. Python 3 doesn't work like that, though: you can't compare a byte string and a unicode string, so in Python 3 you should do the sane thing and only sort lists of one type of string.

>>> lst = ['Aden', u'abe1']
>>> sorted(lst)
['Aden', u'abe1']
>>> sorted(lst, key=lambda s: s.lower())
[u'abe1', 'Aden']

Solution 2

>>> x = ['Aden', 'abel']
>>> sorted(x, key=str.lower) # Or unicode.lower if all items are unicode
['abel', 'Aden']

In Python 3 str is unicode but in Python 2 you can use this more general approach which works for both str and unicode:

>>> sorted(x, key=lambda s: s.lower())
['abel', 'Aden']

Solution 3

You can also try this to sort the list in-place:

>>> x = ['Aden', 'abel']
>>> x.sort(key=lambda y: y.lower())
>>> x
['abel', 'Aden']

Solution 4

This works in Python 3 and does not involves lowercasing the result (!).

values.sort(key=str.lower)

Solution 5

In python3 you can use

list1.sort(key=lambda x: x.lower()) #Case In-sensitive             
list1.sort() #Case Sensitive
Share:
84,484

Related videos on Youtube

jamylak
Author by

jamylak

--

Updated on July 08, 2022

Comments

  • jamylak
    jamylak almost 2 years

    I have a list of strings like this:

    ['Aden', 'abel']
    

    I want to sort the items, case-insensitive. So I want to get:

    ['abel', 'Aden']
    

    But I get the opposite with sorted() or list.sort(), because uppercase appears before lowercase.

    How can I ignore the case? I've seen solutions which involves lowercasing all list items, but I don't want to change the case of the list items.

  • Admin
    Admin about 12 years
    Thank you. I know I should have mentioned this before, but I've heard there's a problem with using this method on a unicode string (Py2). Do you know anything about that?
  • Admin
    Admin about 12 years
    They are all unicode. Thanks! One more question, how to do it on a list like this: [['Aden'], ['abel']]
  • jamylak
    jamylak about 12 years
    Does each list only have one item? If so just modify it a bit to: sorted(x,key=lambda i:i[0].lower())
  • Admin
    Admin about 12 years
    Well, it might have some other stuff as well, which should not be used for sorting though.
  • jamylak
    jamylak about 12 years
    Nevermind, it appears i was wrong, the sorting does work for a mix of both string and unicode, i was confused with a previous question where tuples were also included in the sort.
  • IceArdor
    IceArdor over 10 years
    This solution is overkill and unreadable when a one-liner suffices. This might be more acceptable in a language other than Python.
  • Daniel Andersson
    Daniel Andersson almost 8 years
    One can avoid the lambda function roundtrip by (Python 3) using the general str.lower function as sorted(lst, key=str.lower) or (Python 2) using the lower method of the string module as sorted(lst, key=string.lower). One can also use str.lower for strings in Python 2, but would then have to use unicode.lower for unicode objects, whereas string.lower accepts both (which, as you put it, is probably not really a "sane" mode of operation, though).
  • matth
    matth about 4 years
  • Enterprise
    Enterprise almost 4 years
    This would not work for a list like ['Z', 'B', 'a', 'b', 'A'], which sorts to ['a', 'A', 'B', 'b', 'Z']. The capital 'B' appears before the lowercase 'b' because Python's sort() and sorted() preserve the original order when strings match. In this case the capital 'B' is considered to match the lowercase 'b' when using casefold. This always happens if you convert case in order to compare: sorted(spam, key=str.lower) or sorted(spam, key=str.upper) or sorted(spam, key=str.casefold).
  • Enterprise
    Enterprise almost 4 years
    Try this solution instead: stackoverflow.com/a/1098160/10668287. It will sort ['Aden', 'aden'] correctly as ['aden', 'Aden'].
  • Mike Q
    Mike Q about 2 years
    The biggest issue with this solution is that it violates the rule of using the built-in tested tooling for a home grown solution. It is an interesting response though, I'll give you that.
  • Mike Q
    Mike Q about 2 years
    The biggest issue with this solution is that it violates the rule of using the built-in tested tooling for a home grown solution. It is an interesting response though, I'll give you that.