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']
Answer from user25148 on Stack Overflow
Discussions

case insensitive tuple sorting by an element
Hmm, it might be a typo in your post and not in your code, but I have to take a stab. lower() is a method not an attribute: filelist.sort(key=lambda tup: tup[1].lower()) More on reddit.com
🌐 r/learnpython
7
2
April 12, 2020
python - Case insensitive sorting with sort(list, key=str.lower) - Stack Overflow
I have a module designed to allow users to enter 10 words, then alphabetize them, and display them. Just using the sort functions puts capitalized words first, so i used sort(list, key=str.lower) b... More on stackoverflow.com
🌐 stackoverflow.com
December 12, 2013
sorting - How to sort text strings in Python case-insensitively AND deterministically - Stack Overflow
I have the standard requirement to sort a list of text strings case-insensitively. However additionally this sort needs to be deterministic in the way that two lists containing the same elements sh... More on stackoverflow.com
🌐 stackoverflow.com
python - How to do case insensitive sort of a dictionary and store them in OrderedDict - Stack Overflow
What I want to do is to perform key sorting using case insensitive and stored them in OrderedDict yielding: More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I sort strings case-insensitively in Python?
Use sorted(strings, key=str.lower) or strings.sort(key=str.lower). For international text, use key=str.casefold instead. The key function converts strings to lowercase for comparison only — original capitalisation is preserved in the output. Use sorted() when callers or other code may need the original list unchanged. Use .sort() when the list is owned by the current operation and mutating it is intentional. Both accept reverse=True for descending order.
🌐
openpython.org
openpython.org › home › articles › python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically ...
Why does Python sort uppercase before lowercase by default?
Python compares strings using Unicode code points. Uppercase letters (A=65 to Z=90) have lower code point values than lowercase letters (a=97 to z=122), so "Z" < "a" evaluates to True. This is correct Unicode ordering but not human alphabetical order. Use key=str.lower or key=str.casefold to get alphabetical order. String comparison is lexicographic: it compares the first differing code point, then only examines later characters if necessary. That is why the capitalization of the first character can dominate the entire result.
🌐
openpython.org
openpython.org › home › articles › python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically ...
How do I sort strings case-insensitively in descending order?
Combine key=str.lower (or casefold) with reverse=True: sorted(words, key=str.lower, reverse=True). This sorts Z to A ignoring case. Do not reverse the characters in each string; descending order reverses item placement, not spelling. If you add a secondary tuple key, test the tie behavior because the chosen direction may affect how ties are perceived.
🌐
openpython.org
openpython.org › home › articles › python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically ...
🌐
Programming Idioms
programming-idioms.org › idiom › 297 › sort-a-list-of-strings-case-insensitively › 5479 › python
Sort a list of strings, case-insensitively, in Python
func lessCaseInsensitive(s, t string) bool { for { if len(t) == 0 { return false } if len(s) == 0 { return true } c, sizec := utf8.DecodeRuneInString(s) d, sized := utf8.DecodeRuneInString(t) lowerc := unicode.ToLower(c) lowerd := unicode.ToLower(d) if lowerc < lowerd { return true } if lowerc > lowerd { return false } s = s[sizec:] t = t[sized:] } } sort.Slice(data, func(i, j int) bool { return lessCaseInsensitive(data[i], data[j]) })
🌐
Reddit
reddit.com › r/learnpython › case insensitive tuple sorting by an element
r/learnpython on Reddit: case insensitive tuple sorting by an element
April 12, 2020 -

EDIT: Thanks to SHxKM and cybersection for pointing out that I called lower and not lower(). Very different and the latter fixed my issue.

So I am having a bit of a weird issue here and the normal methods I'm seeing online don't seem to work out.

So I've got a big unsorted list of tuples that I need to sort. For the sake of example let's say that the first element is file mode and the second is the filename. I want the directories on top and the files on the bottom. So I create 2 empty lists (dirlist and filelist) and used this sort of thing:

for i, key in enumerate(unsorted_list):if key[0] == "040000":dirlist.append(formatted_content[i])else:filelist.append(formatted_content[i])

So to combine these into one list I just use something like:

sorted_list = dirlist + filelist

That puts the directories on top and the file on the bottom fine, but they're not sorted, and an alphabetical list would be nice.

So I tried inserting this before the sorted_list code above:

dirlist.sort(key=lambda tup: tup[1])filelist.sort(key=lambda tup: tup[1])

Well, that does, indeed, sort in a manner of speaking, but it's case sensitive, so all the capitalized files floated to the top and THAT is my problem.

Sorting case insensitive is usually done with a lambda, and I've already got one.

Initially I thought about sorting by the key and then sorting by the other key, but, of course, all that does it sort it twice.

I also tried adding a lower to the tup sorting like:

filelist.sort(key=lambda tup: tup[1].lower)

That certainly does SOMETHING, but it isn't alphabetizing, that's for sure (near as I can tell it's putting the lower cased stuff on top, but now really doing any sorting beyond that?).

Anyway, I have been spinning my wheels on this for the better part of four hours, so I thought I'd poke in here and ask for some better guidance.

🌐
Learn By Example
learnbyexample.org › python-list-sort-method
Python List sort() Method - Learn By Example
December 22, 2022 - If you want to sort the values ... 'Green', 'orange', 'Red'] This causes the sort() function to treat all the list items as if they were lowercase without actually changing the values in the list....
🌐
W3Schools
w3schools.com › python › python_lists_sort.asp
Python - Sort Lists
Luckily we can use built-in functions as key functions when sorting a list. So if you want a case-insensitive sort function, use str.lower as a key function:
Find elsewhere
🌐
YouTube
youtube.com › watch
Python short | perform Case Insensitive sort of python list #shorts - YouTube
Learn within a minute how to sort a Python list case insenstively.Watch the entire playlist of Python one-liners to rock your next interviewhttps://www.youtu...
Published: February 23, 2022
Views: 199
🌐
OpenPython
openpython.org › home › articles › python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically | OpenPython
1 week ago - Learn how to sort Python strings case-insensitively using key=str.lower, str.casefold, and locale-aware sorting. Covers ascending, descending, lists of dicts...
🌐
Autodesk
help.autodesk.com › cloudhelp › CHS › MayaCRE-Tech-Docs › CommandsPython › sortCaseInsensitive.html
sortCaseInsensitive command
Python examples. ... Note: Strings representing object names and arguments must be separated by commas. This is not depicted in the synopsis. sortCaseInsensitive is NOT undoable, NOT queryable, and NOT editable. This command sorts all the strings of an array in a case insensitive way.
🌐
YouTube
youtube.com › watch
Python Programming 30 - Case Insensitive Sort - YouTube
Start your software dev career - https://calcur.tech/dev-fundamentals 💯 FREE Courses (100+ hours) - https://calcur.tech/all-in-ones🐍 Python Course - https:...
Published: August 30, 2020
🌐
Python
python-list.python.narkive.com › WEfapLZl › case-insensitive-and-internationalized-sort
case-insensitive and internationalized sort
If a named parameter is going to be added to the sort method, it would probably require a PEP and discussion on python-dev before it was accepted. But since sort() doesn't do what a lot of people expect I would like to discuss the issues here first. This topic came out of a off-list discussion I've been having with Jarno J Virtanen, who supplied the following case-insensitive function and test code: def compare(a, b): return cmp(a.upper(), b.upper()) Now, if I test it with the following list: s = [u'?', u'?', u'?', 'b', 'a', 'B', u'a', 'A'] s.sort(compare) for c in s: print c.encode('latin-1'), print it yields: a a A b B ?
🌐
SciPython
scipython.com › books › book2 › chapter-4-the-core-python-language-ii › examples › sorting-methods
E4.12: Sorting methods
For example, sorting a list of strings is case-sensitive by default: >>> sorted('Nobody expects the Spanish Inquisition'.split()) ['Inquisition', 'Nobody', 'Spanish', 'expects', 'the'] We can make the sorting case-insensitive, however, by passing each word to the str.lower method:
🌐
Python
python-list.python.narkive.com › UQVb86Le › sort-list-of-dictionaries-by-key-case-insensitive
Sort list of dictionaries by key (case insensitive)
Unfortunately, I only have Python 2.3.5 installed and can't upgrade to 2.4 due to an underliying application server. In python 2.3 the 'sort()' function does not excepts any keywords arguments (TypeError: sort() takes no keyword arguments), so is there a workaround?
🌐
ActiveState
code.activestate.com › recipes › 286204-case-insensitive-sort
Case Insensitive Sort « Python recipes « ActiveState Code
July 7, 2004 - This is a recipe that does a case insensitive sort. The normal sort methods of lists has 'B'<'a', which means that it would sort 'Pear' to come before 'apple' in a list. You can pass in a function to the sort method to change this... but this can be slow. This is a function that transforms the list, uses the sort method and then transforms it back. ... In the caseless module I am using this to become the sort method for a subclass of list. That class depends on python 2.2 as it subclasses list - but this function ought to work on versions earlier than that.
🌐
GeeksforGeeks
geeksforgeeks.org › python-sort-strings-by-case-difference
Python – Sort Strings by Case difference | GeeksforGeeks
April 21, 2023 - # Python3 code to demonstrate working of # Sort Strings by Case difference # Using Bubble Sort Algorithm # initializing Matrix test_list = ["GFG", "GeeKs", "best", "FOr", "alL", "GEEKS"] # printing original list print("The original list is : " + str(test_list)) # sorting using Bubble Sort Algorithm n = len(test_list) swapped = True while swapped: swapped = False for i in range(n - 1): if abs(len([ele for ele in test_list[i] if ele.islower()]) - \ len([ele for ele in test_list[i] if ele.isupper()])) > \ abs(len([ele for ele in test_list[i+1] if ele.islower()]) - \ len([ele for ele in test_list[i+1] if ele.isupper()])): test_list[i], test_list[i+1] = test_list[i+1], test_list[i] swapped = True n -= 1 # printing result print("Sorted Strings by case difference : " + str(test_list))
🌐
Coding
studycode3.wordpress.com › 2023 › 03 › 31 › 65-case-insensitive-sorting-in-python
65. Case-Insensitive Sorting in python – Coding
March 31, 2023 - Case-Insensitive Sorting in python refers to a way of sorting strings in a case-insensitive manner, which means that the sorting algorithm treats uppercase and lowercase letters as equivalent. This…