Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python sort dictionary case insensitive

# 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 by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #python #sort #dictionary #case #insensitive
ADD COMMENT
Topic
Name
6+9 =