└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Python One liners 2 | This github repo is a collection of amazing python one liners which I regularly use which saved me a lot of time. 3 | Feel free to add more to the list. 4 | 5 | 6 | # Contents 7 | * [Sort a dictionary based on multiple conditions](#Sort-a-dictionary-based-on-multiple-conditions) 8 | * [Thousand Separator](#Thousand-Separator) 9 | * [Multiply all elements of a list](#Multiply-all-elements-of-a-list) 10 | * [Flatten a List](#Flatten-a-List) 11 | * [Remove duplicate elements from a list](#Remove-duplicate-elements-from-a-list) 12 | * [Get quotient and remainder](#Get-quotient-and-remainder) 13 | * [Most frequent element of a List](#Most-frequent-element-of-a-List) 14 | * [Convert string to upper case](#Convert-string-to-upper-case) 15 | * [Converting string to byte](#Converting-string-to-byte) 16 | * [Transpose a Matrix](#Transpose-a-Matrix) 17 | * [Element wise addition of 2 lists](#Element-wise-addition-of-2-lists) 18 | * [Convert ASCII value to Character and vice versa](#Convert-ASCII-value-to-Character-and-vice-versa) 19 | * [Print all combinations of elements in a List](#Print-all-combinations-of-elements-in-a-List) 20 | * [Input space separated integers in a list](#Input-space-separated-integers-in-a-list) 21 | * [Nested for loops via List comprehension](#Nested-for-loops-via-List-comprehension) 22 | * [Find All Indices of an Element in a List](#Find-All-Indices-of-an-Element-in-a-List) 23 | * [Convert number of any base to decimal](#Convert-number-of-any-base-to-decimal) 24 | 25 | 26 | ### Sort a dictionary based on multiple conditions 27 | 28 | ```python 29 | d={'IN':2, 'GE':2, 'AK':3, 'BEG':1} 30 | 31 | n_l=sorted(d.items(), key=lambda x: (x[1], len(x[0]),x[0])) 32 | 33 | new_d={k:v for k,v in n_l} 34 | 35 | # new_d = {'BEG': 1, 'GE': 2, 'IN': 2, 'AK': 3} 36 | ``` 37 | 38 | ### Thousand Separator 39 | 40 | ```python 41 | n=10000000 42 | 43 | new_number=f'{n:,}' 44 | 45 | # new_number = 10,000,000 46 | ``` 47 | 48 | ### Multiply all elements of a list 49 | 50 | ```python 51 | from numpy import prod 52 | from functools import reduce 53 | 54 | l=[2,3,4,4,5,6] 55 | 56 | v1=prod(1) # 2880 57 | 58 | v2=reduce((lambda x, y: x * y), l) #2880 59 | ``` 60 | 61 | ### Flatten a List 62 | 63 | ```python 64 | l = [[7,6], [4, 6], [8, 10]] 65 | 66 | flattened = [i for j in l for i in j] 67 | 68 | # flattened = [7, 6, 4, 6, 8, 10] 69 | ``` 70 | 71 | ### Remove duplicate elements from a list 72 | 73 | ```python 74 | l = [4,4,5,5,6] 75 | 76 | new_l=list(set(l)) 77 | 78 | # new_1 = [4,5,6] 79 | ``` 80 | 81 | ### Get quotient and remainder 82 | 83 | ```python 84 | quotient, remainder = divmod(10,5) 85 | 86 | # quotient, remainder = 2,0 87 | ``` 88 | 89 | ### Most frequent element of a List 90 | 91 | ```python 92 | from collections import Counter 93 | 94 | l = [1,1,3,4,1,5,6,7,7,2,9] 95 | 96 | freq_ele=Counter(l).most_common()[0][0] 97 | 98 | # freq_ele = 1 99 | ``` 100 | 101 | ### Convert string to upper case 102 | 103 | ```python 104 | "hi my name is Allwin".upper() 105 | 106 | # 'HI MY NAME IS ALLWIN' 107 | ``` 108 | 109 | ### Converting string to byte 110 | 111 | ```python 112 | s="My name is fox and fox is an animal" 113 | 114 | s.encode() 115 | 116 | # b'My name is fox and fox is an animal' 117 | ``` 118 | 119 | ### Transpose a Matrix 120 | 121 | ```python 122 | matrix = [[1, 2, 3], [3, 4, 6], [5, 6, 7]] 123 | 124 | l=list(list(x) for x in zip(*matrix)) 125 | 126 | # l = [[1, 3, 5], [2, 4, 6], [3, 6, 7]] 127 | ``` 128 | 129 | ### Element wise addition of 2 lists 130 | 131 | ```python 132 | first = [1,2,3,4,5] 133 | 134 | second = [6,7,8,9,10] 135 | 136 | final=[x + y for x, y in zip(first, second)] 137 | 138 | # final = [7, 9, 11, 13, 15] 139 | ``` 140 | 141 | ### Convert ASCII value to Character and vice versa 142 | 143 | ``` 144 | ord('a') # 97 145 | 146 | chr(97) # a 147 | 148 | chr(ord('a') + 3) #d 149 | ``` 150 | 151 | ### Print all combinations of elements in a List 152 | 153 | ```python 154 | from itertools import combinations, combinations_with_replacement 155 | 156 | nums = [1,2,3,4] 157 | 158 | list(combinations(nums, 2)) 159 | # [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] 160 | 161 | list(combinations_with_replacement(nums, 2)) 162 | # [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)] 163 | 164 | list(combinations (nums, 3)) 165 | # [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] 166 | ``` 167 | 168 | ### Input space separated integers in a list 169 | 170 | ```python 171 | l = list(map(int, input().split())) 172 | ``` 173 | 174 | ### Read file in python and input it to a list 175 | 176 | ```python 177 | l = [line.strip() for line in open('abc.txt', 'r')] 178 | ``` 179 | 180 | ### Nested for loops via List comprehension 181 | 182 | ```python 183 | l1 = [1, 2, 3, 4] 184 | 185 | l2 = ['a', 'b', 'c'] 186 | 187 | l = [(x, y) for x in l1 for y in l2] 188 | 189 | # [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), (4, 'a'), (4, 'b'), (4, 'c')] 190 | ``` 191 | 192 | ### Find All Indices of an Element in a List 193 | 194 | ```python 195 | lst = ['a', 'v', 'a', 'c', 'z'] 196 | 197 | indices = [i for i in range(len(lst)) if lst[i]=='a'] 198 | 199 | # [0, 2] 200 | ``` 201 | 202 | ### Convert number of any base to decimal 203 | 204 | ```python 205 | int('30', 8) # 24 206 | 207 | int('1011', 2) # 11 208 | 209 | int('1A', 16) # 26 210 | ``` 211 | --------------------------------------------------------------------------------