4. Building Lists syntax question


#1



https://www.codecademy.com/courses/python-beginner-en-KAgt5/1/1?curriculum_id=4f89dab3d788890003000096


Hello! i dont understand the syntax on this example, like, here:

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

why is it necessary that extra i in: i for i in range(51) ?
isnt it just enough for i in range(51) ?

thanks for the answers!



#2

the list comprehension you are using is basically a shorthand for this: (ignoring the if condition for a moment)

even_to_50 = []
for x in range(51):
    even_to_50.append(x)

so the first x (or in your case i) is what you are appending to the list, so you need it


#3

ohh i got it, basically that extra i in my example indicates whats is going to fill the list
thanks!


#4

yes exactly, that is like the perfect description.


#5

awesome :slight_smile:
thanks man