List Comprehensions: Conditional lesson in the Python pro course

Just a little confused with one of the lessons in the Python course. It’s currently discussing list comprehensions and conditionals, and states an example list of heights as the exercise. The task is to create a new list and include every element from the heights list that is greater than 161. I become confused with the syntax of the list comprehension expression and the use of the for loop involved, see below in the link;
List Comprehension: Conditionals

Hi,
They do take some getting used to.
I find it’s easier to split them into three parts

lst = [ (what’s going in the list) (the loop) (condition if needed) ]

So, if I wanted all numbers less than 5 from the list;
old_list = [3, 7, 9, 4, 78, 12, 0]

what’s going in the list:
let’s call it num and we’ll use that in the loop

the loop:
using a for loop to go through old_lst, and we want num to be used as the iterator, so;
for num in old_list

condition (if needed):
we only want those numbers less than 5, so;
if num < 5

Then, put it all together;
new_lst = [num for num in old_lst if num < 5]

or,
if you just wanted to double all the numbers in old_lst, you could do;
new_lst = [num * 2 for num in old_lst]

It’s a good concise way to create lists and well worth knowing, but can take a little while to get used to.

hope that helps

Hello, firstly thanks for the reply. I really appreciate the help you’ve put into that response. The main issue I have is with the following of the expression;

num for num

I don’t understand why we are using the iterator twice at the beginning? What would be the difference if we did;

new_lst = [for num in old_lst if num < 5]

Hi,
Like in my second example, it means you can then do something else to it before you put it into the list.