Search
 
SCRIPT & CODE EXAMPLE
 

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']
Comment

PREVIOUS NEXT
Code Example
Python :: python quiz answer stores 
Python :: python class declaration 
Python :: label encoding of a column in python 
Python :: print function python 
Python :: install multiple versions of python 
Python :: pandas previous row 
Python :: Python NumPy column_stack Function Syntax 
Python :: python range function examples 
Python :: python one sample t-test 
Python :: python tkinter get entry text 
Python :: create period pandas 
Python :: robot framework log from python 
Python :: optimize python code 
Python :: renamecolumns pandas 
Python :: how to block a ip adress 
Python :: x = 10 x += 12 y = x/4 x = x + y in python 
Python :: remove percentage in python 
Python :: how to get github repository access in python code directly 
Python :: how to print 2d neatly in python 
Python :: rstudi matplotlib crash qt 
Python :: python enforcing class variables in subclass 
Python :: pypi modules for 3d gui 
Python :: how to upgrade pip 
Shell :: amazon linux 2 install stress 
Shell :: ubuntu uninstall redis 
Shell :: kill process running on port mac 
Shell :: install nodemon as dev dependency 
Shell :: centos stop apache 
Shell :: install curl on ubuntu 
Shell :: uninstall npm ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =