Case Insensitive Sort Python

[Solved] Case Insensitive Sort Python | Ruby - Code Explorer | yomemimo.com
Question : python sort case insensitive

Answered by : charlesalexandre-roy

# Basic syntax:
sorted(your_list, key=str.lower)
# Where:
#	- the key parameter specifies a function (or other callable) that is
#	called on each list element prior to making comparisons. Here, we
#	sort your_list as though all the elements were lowercase
# Example usage 1:
your_list = ["Z", "zebra", "A", "apple"]
sorted(your_list) # without key
--> ['A', 'Z', 'apple', 'zebra']
sorted(your_list, key=str.lower) # with key
--> ['A', 'apple', 'Z', 'zebra']
# Example usage 2:
# Say you want to sort the keys of a dictionary by their last letter:
your_dictionary = {'this': 3, 'example': 4, 'be': 5, 'banana': 1}
sorted(your_dictionary.keys(), key=lambda i: i[-1])
--> ['banana', 'example', 'be', 'this']

Source : https://www.geeksforgeeks.org/python-ways-to-sort-list-of-strings-in-case-insensitive-manner/ | Last Update : Thu, 28 Apr 22

Answers related to case insensitive sort python

Code Explorer Popular Question For Ruby