I don't quite understand how this ' for ' loop works in this list:
lista_1 = ['a', 'b', 'c']
lista_2 = [i for i in lista_1]
print(lista_1)
If I understand the loop, what I don't understand is why before the ' for ' loop there is the ' i ' if I remove it it gives an error, so my question is why is the ' i ' placed in that place?
Thanks in advance for your answers!
What you're seeing is a syntax called generator expression . These types of expressions generate a series of values, for which they internally use a loop
for
, but the values they generate can be computed with an arbitrary expression.The general syntax of the generator expression is:
The
for
causes an iteration overiterable
whatever you put at the end (which can be a list, or anything else that can be iterated over). On each iteration, the variableelemento
takes a value from that iterable. Then the oneexpresion
that appears at the beginning is evaluated. Usually thatexpresion
contains some form of theelemento
, like for example here:In this case the iterable is
range(5)
, which returns the numbers0, 1, 2, 3
and4
, which are the values that thei
. But the expression puts(i+1)*2
, so that expression will take the values2, 4, 6, 8,
and10
. The generating expression will therefore create the latter.When you enclose a generator expression in square brackets, you have what is called a list comprehension , which simply means that a list is created from the data generated by the generator expression. In the example above, the list would be created
[2, 4, 6, 8, 10]
So your case is explained:
The expression in this case is
i
, that is, each element is left as it was, without transforming. So in the end itlista_2
will have the same elements aslista_1
. In this case the assignment is equivalent tolista_2 = list(lista_1)
. Note that it is not the same aslista_2 = lista_1
since in this second case we would be making both lists refer to the same object in memory, while in the first we are creating a new list, yes, with the same elements as the first.additional details
The expression does not have to contain
i
. For example the following would be valid:In this case the expression is
"hola"
, so a new list would be created with the word"hola"
repeated as many times as there are elementslista_1
.Generator expressions also admit another form, with a condition at the end, like this:
In this case, if the condition is true, the expression is evaluated and added to the result, but if the condition is false, it is not.
For example, the following assignment would copy
lista_2
only the uppercase letters inlista_1
:Generator expressions can be even more complicated, as they support
for
nesting.If the generator expression is enclosed in parentheses instead of brackets, then no list is created, but rather the entire expression is another iterator that can be iterated over, or passed as a parameter to functions that expect an iterator.
with instruction
you iterate through the list
lista_1
, plugging ini
each of its elements on each iteration.By placing the
i
again in front of the , you add each of the values that it takesfor
to a new listlista_2
i
Maybe with a list and a tuple you'll see it better: