Building Lists comprehension


#1

why we have to wright i before FOR in the iteration

evens_to_50 = [i for i in range(51) if i % 2 == 0]


#2

@wasimsami

Well, the first item is what it will put into the list each iteration. It is just the syntax of list comprehension.

print(['O' for i in range(5)])
print('O' for i in range(5))

print({(x, y): 'Empty' for x in range(5) for y in range(5)})

As you can see it is simply the syntax of that particular code. You do not have to use the iterator you use to generate as I showed with the 'O', for each iteration it will just put the 'O' in instead of the iterator.