Python Comprehensions

Rasegaprabakar
2 min readApr 10, 2022

Hi All,

We all agree that we always choose fast and efficient methodologies in everything. Python also provides us the amazing ,exciting, fast and efficient methodologies for efficient people. One of the efficient way which python yields to developer is Comprehensions.

I assure you will be more addicted to python once you start works on comprehensions. Yeah ! I am addicted to comprehensions now. I would like to share few simple and twisty python comprehensions here.

How normal loop structure looks like,

creating list by iterating the elements using loops

We can change the loop structure into simple one, may be one line structure as comprehensions.

Creating list by list comprehension

Exciting ? Lot more we have, we can also include condition in comprehensions.

list comprehension with if statement

Looking for dictionary comprehension ? Let’s see that too..

Dictionary comprehension

We missed one thing, right ? Yeah ! Complex problem needs complex loops.

Any dimensional loops can be comprehensive in Python. Don’t want to try ?

Two dimensional Loops

Hope this will make you curious on Comprehensions in Python.

Code and Enjoy !!!

--

--