Python One-Liners for seemingly complex tasks

Save time & effort implementing these in your day to day coding sessions

Photo by James Harrison on Unsplash

Sort a dictionary based on multiple conditions

The above code sorts the dictionary based on the value first (In this case 2,2,3,1), if more than 2 keys have the same value then the sorting takes place based on the length of the key (Since there are 2 keys with the same value as 2, the sorting now takes place based on the length of the keys IN & GE) if the lengths of the 2 keys are the same then the sorting takes place finally on the key itself (Here the keys IN & GE are of the same length thus they will be sorted alphabetically) which gives us the final dictionary.

Thousand Separator

Multiply all elements of a list

Flatten a List

Remove duplicate elements from a list

Get quotient and remainder

Most frequent element of a List

Converting string to byte

Transpose a Matrix

Element wise addition of 2 lists

Convert ASCII value to Character and vice versa

Print all combinations of elements in a List

Input space separated integers in a list

Read file in python and input it to a list

Nested for loops via List comprehension

Find All Indices of an Element in a List

Convert number of any base to decimal

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store