Building Lists


#1

Hello!

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

Does someone know what this first “i” stands for? My understanding of this is quite the same as in Topic I attached.

Found that old topic, but unfortunately no one is sure in their answer.
https://www.codecademy.com/en/forum_questions/50e9c3e245d1bdf24d00b22f


#2

if we where to convert the list comprehension to “normal” code:

even_to_50 = []
for i in range(51):
   if i % 2 == 0:
      even_to_50.append(i)

the i you are asking about is the i used in append, lets say we want to the square of each number:

even_to_50 = []
for i in range(51):
   if i % 2 == 0:
      even_to_50.append(i ** 2)

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

hope that answers your question


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.