values = int(i) for i in values
The above code with throw a syntax error because it is not correct Python syntax (for list comprehension). int(i) for i in values
is a completely incorrect statement because there is no way Python knows how to combine, or construct, those 2 statements.
List comprehension statements are surounded by brackets [ ]
, because those are what enclose lists (I'm sure you know that [1, 2, 3]
is a list).
values = [int(i) for i in values]
The above code uses list comprehension because it is surrounded by [ ]
. The statement for i in values
will go though the list called values
, and one by one, assign a value to the variable I
, for you to do something with. In this case, int(i)
will convert the variable I
and append, or add, to the values
list. This line of code will in the end convert all the values in values
to integers.