Python list comprehension pros and cons

Python: List comprehension vs Generator expression

Laksh Samdariya
Follow
Oct 16 · 7 min read

Python! We have heard this name a lot since this pandemic. From small children to us [developers], all have been learning and developing software and programs using it. So, is python so easy?

It might be easy compared to other programming languages like C/C++/Java/Ruby etc. but, its not as easy as accessing Spotify or Instagram [developed using Python]. It has its fair share of challenges and difficulties however this doesnt mean you cant learn it. Learning python is an art and I am your artist here, Im going to show you a small hack to make sure your program runs smoothly using List comprehensions or Generator expressions.

So when I started studying Python, the first thing that I came across was FOR loops, used a lot. At first, I found them to be complicated and a bit difficult [as a beginner]. Another thing I did not like was WHILE loops, which made it even more confusing to me. I was told that what a FOR loop can do, the same can be done by WHILE loop but vice versa is not possible. An immediate question popped up in my head Bhai agar WHILE loop FOR loop ka kaam karra hai plus additional kaam kar raha hai why not just use it? Well, the answer is simple, you need to get through a lot of complications to make a WHILE loop work and it occupies a lot more space than FOR [space in the sense.. Increases the length of the code. Codes are supposed to be shorter since for programmers shorter is sweeter]. So, I got through it and learned how to use FOR loops.

After this, I came across LIST. Everything was going well until I started learning List comprehensions. It just blew my mind. It was confusing because of the multiple ways to construct a List comprehension using conditions before and after and soo much more. Now, this started to be a headache for me. [PROGRAMMING IS NEVER EASY]. I Struggled a lot to understand it and finally did.

So now, lets get out of my story, and lets see what LIST COMPREHENSIONS are.

LIST COMPREHENSION:

List comprehension gives us a shorter syntax compared to for loop when you want to create a new loop based on the values of some existing loop.

List comprehension is used when we want to create a new list whose values depend upon the values of some existing list in your program.

It has 3 benefits:

  1. It makes your code shorter.
  2. It makes it easy to understand.
  3. Creates a list automatically without you asking it to make one.

Now well see some examples to get a better understanding.

Ex 1: Here, we have used a for loop and the code seems to be big. Writing big programs would be a waste of time as we have to write soo many additional lines.

names = ["vidhanshu","vidit","sobhan","laksh"]empty = []for name in names: if name[0] == 'v': empty.append[name]print[empty]

OR

We have done the same with a single line of code.

names = ["vidhanshu","vidit","sobhan","laksh"][name for name in names if name[0] == 'v']

Ex 2: Write a program that prints numbers less than 6 when we pass in numbers 110.

Hold on try it out on your own and then refer below.

You can solve the above problem by both these methods but the recommended one is list comprehension.

eemp = []for num in range[10]: if num < 6: eemp.append[num]print[eemp]

OR

eemp = [num for num in range[10] if num

Chủ Đề