What is the best way to sort a list of words with the case insensitive?

Recommended Answers

All 2 Replies

Hi!

Since Python 2.4 (I guess it's 2.4), sort has the option key:

>>> import string
>>> l = ["a","B","c","D"]
>>> l.sort(key=string.lower)
>>> l
['a', 'B', 'c', 'D']

Here's a way for < 2.4:

>>> l.sort(lambda x, y: cmp(string.lower(x), string.lower(y)))

Regards, mawe

Mawe, I put your suggestions into program and they work well!

Thank you much.

Just started with Python so i am using 2.4

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.