42

I have a list which contains strings representing animal names. I need to sort the list. If I use sorted(list), it will give the list output with uppercase strings first and then lowercase.

But I need the below output.

Input:

var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']

Output:

['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Darknight
  • 1,132
  • 2
  • 13
  • 31

3 Answers3

62

The sort() method and the sorted() function take a key argument:

var.sort(key=lambda v: v.upper())

The function named in key is called for each value and the return value is used when sorting, without affecting the actual values:

>>> var=['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
>>> sorted(var, key=lambda v: v.upper())
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

To sort Ant before ant, you'd have to include a little more info in the key, so that otherwise equal values are sorted in a given order:

>>> sorted(var, key=lambda v: (v.upper(), v[0].islower()))
['Ant', 'ant', 'Bat', 'bat', 'Cat', 'cat', 'Goat', 'Lion']

The more complex key generates ('ANT', False) for Ant, and ('ANT', True) for ant; True is sorted after False and so uppercased words are sorted before their lowercase equivalent.

See the Python sorting HOWTO for more information.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks Martijin. Is ther way to get output also as ['Ant', 'ant', 'Bat', 'bat', 'Cat', 'cat', 'Goat', 'Lion']. I have tried var.sort(key=lambda v: v.lower()). It gives the same output – Darknight Dec 19 '12 at 15:03
  • 1
    @PSivachandran: You have to add a little info to the key to make `Ant` sort before `ant`. Use `lamda v: (v.upper(), v[0].islower())` for example, as `True` is sorted after `False`. – Martijn Pieters Dec 19 '12 at 15:06
  • 2
    Better use `sorted(var, key=lambda v: (v.upper(), v))` – coldfix Feb 08 '16 at 02:03
  • @coldfix: that depends; is it a requirement that the lowercase version of a word comes before the same word with title caps even if their order in the original was swapped? Then yes, return a tuple. – Martijn Pieters Feb 08 '16 at 07:39
  • 2
    OFC, but I think it's a bit of a stretch that someone would want to sort case-insensitively on the whole word and use the case of the first letter for edge cases but then suddenly stop there and use stability for the rest:) If someone were to write *that code* I'd be 99% sure that they actually wanted the simpler solution from my comment above which makes the order of the output deterministic from the set of input elements independent of their initial order (so you could e.g. use this to sort on `set`s etc). This is fulfilled netiher by `key=str.upper` nor by your last suggestion. – coldfix Feb 08 '16 at 12:19
  • @coldfix There's also the possibility that they just wanted upper case versions to appear first, in which case `sorted(var, key=lambda v: (v.upper(), v.swapcase()))` might work well. – Casey Kuball Dec 02 '16 at 20:46
27

New answer for Python 3, I'd like to add two points:

  1. Use str.casefold for case-insensitive comparisons.
  2. Use the method directly instead of inside of a lambda.

That is:

var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']

var.sort(key=str.casefold)

(which sorts in-place) and now:

>>> var
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

Or, to return a new list, use sorted

>>> var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
>>> sorted(var, key=str.casefold)
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

Why is this different from str.lower or str.upper? According to the documentation:

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, str.lower() would do nothing to 'ß'; casefold() converts it to "ss".

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
5

I need to add yet another answer, since both the accepted answer and the newer versions lack one important thing:

The here proposed case-insensitive sorting is not stable in the ordering of "equal" keys!

That means: When you have a mixture of mixed case strings that you want to sort, you get a correctly sorted list, but it is undefined whether "AbC" comes before "aBc" or after. This may even vary between runs of the same program.

In order to always have the same output with a stable default ordering of strings, I use the following function:

sorted(var, key=lambda v: (v.casefold(), v))

This way, the original key is always appended as a fallback ordering when the casefold version does not supply a difference to sort on.

Christian Tismer
  • 1,305
  • 14
  • 16