Today you’re going to learn about , a very powerful feature of python to use when creating a based on certain . python list comprehension python List constraints let’s get started Imagine you want to create a list of of numbers ranging from to , Generating the list cubic of numbers using would normally look like this. cubics 1 100 without list comprehension List cubics without list comprehension >>> cubics = [] >>> number range( , ): ... cubic = number** ... cubics.append(cubic) ... >>> cubics [ , , , , , , , , , , , , , , , , , , , , , , ...... ] for in 1 101 3 1 8 27 64 125 216 343 512 729 1000 1331 1728 2197 2744 3375 4096 4913 5832 6859 8000 9261 10648 List of cubics with list comprehension >>> cubics = [number** number range( , )] >>> print(cubics) [ , , , , , , , , , , , , , , , , , , , , , , , , , .... ] 3 for in 1 101 1 8 27 64 125 216 343 512 729 1000 1331 1728 2197 2744 3375 4096 4913 5832 6859 8000 9261 10648 12167 13824 15625 As we can see, we did the with but instead of doing it in of code, we did it in just . is it cool? same thing list comprehension 4 lines one If you’re a fan of then should be your friend, it can do stuff in just one line. one-liners list comprehension amazing List comprehension Syntax List = [element element iterable] for in More examples: Cartesian Product using list comprehension If A and B are two , then their product A × B is the set of all of elements from A and B, with generate a list of all pairs in just one line. non-empty sets cartesian ordered pairs list comprehension >>> a = [ , , , ] >>> b = [ , , , ] >>> product = [(i , j) i a j b] >>> print(product) [( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ), ( , ) ] 1 3 5 7 2 4 6 8 for in for in 1 2 1 4 1 6 1 8 3 2 3 4 3 6 3 8 5 2 5 4 5 6 5 8 7 2 7 4 7 6 7 8 Conditional blocks within a list comprehension Apart from just over , you can also put such as within logic to select the elements based on certain conditions. iterating sequences conditional statements if and else comprehension Even number using list comprehension Let’s say we want to make a list of from using , it can also be done in as shown below; even numbers 203 to 289 list comprehension one-line >>> print(even_numbers) [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 282 284 286 288 Filtering iterable using list comprehension can also be employed to out a given list based on certain For instance, let's use to it to out all candidates name that is above list comprehension filter parameters. filter 18 >>> candidates = [ ... ( , ), ... ( , ), ... ( , ), ... ( , ), ... ( , ) ... ] >>> >>> above_18 = [candidate candidate candidates candidate[ ]>= ] >>> print(above_18) [( , ), ( , ), ( , )] 'John' 15 'Silyvia' 34 'Hamis' 17 'Alphonce' 22 'Grace' 27 for in if 1 18 'Silyvia' 34 'Alphonce' 22 'Grace' 27 Loading certain files using list comprehension list comprehension can also be used in loading multiple file paths to memory for instance we can use it to load all image paths in our current directory just as shown below; >>> os >>> images = [img img os.listdir() img.endswith( )] >>> print(images) [ ] import for in if '.jpg' 'AI gallery.jpg' Flattening 2D array using list comprehension You can also use to flatten the array to a single array by iterating on all elements on a list. list comprehension 2D 1D >>> data =[( , ), ( , ), ( , )] >>> flattened = [n in_data data n in_data] >>> print(flattened) [ , , , , , ] 1 2 3 4 5 6 for in for in 1 2 3 4 5 6 Scalar Product using List Comprehension can also be used to find the of an by by a given number on each element in an independently as shown below; List comprehension scalar iterable multiplying iterable >>> vector = [ , , ] >>> vector3 = [n * n vector] >>> print(vector) [ , , ] 10 25 32 3 for in 10 25 32 The can be found on Original Article kalebujordan.com In case of any suggestion or comment, drop it on the comment box and I will get back to you ASAP.