How these python for loop and slicing working?
Question:
-
How this statement will work?
j for i in range(5)
Example:
x1=(j for i in range(5))
will yield five 4’s when iterated. Why it will not yield 01234 like when we replace j with i.
-
How this statement working?
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1])
0
1
2
2
Answers:
Your first example is a generator expression (the parentheses are required, so the first version you show doesn’t actually make sense). The generator expression repeatedly yields j
, which is not defined in the code you show. But from your description, it already has the value 4
in the environment you were testing in, so you see that value repeatedly. You never use the i
value, which is what’s getting the values from the range
.
As for your other loop, for a[-1] in a
keeps rebinding the last value in the list, and then printing it.
A for loop does an assignment to the target you give it between for
and in
. Usually you use a local variable name (like i
or j
), but you can use a more complicated assignment target, like self.index
or, as in this case, a[-1]
. It’s very strange to be rewriting a value of your list as you iterate over it, but it’s not forbidden.
You never get 3
printed out, because each of the previous assignments as you iterated overwrote it in the list. The last iteration doesn’t change the value that gets printed, since you’re assigning a[-1]
to itself.
Your first question raises error when iterating, as j
is not defined. If you are getting any response from that, you probably have defined a value for j
above your generator, which sounds to be 4
in your code.
About your second question:
when you have something like this:
for i in a:
...
Actually you are are doing this in each cycle: i=a[0], i=a[1], ...
.
When you write a[-1] in a
, it is kind of equal to: a[-1]=a[0], a[-1]=a[1], ...
and you are changing last element of a
each time. So at the end, the last element of your list would be 2
.
-
How this statement will work?
j for i in range(5)
Example:
x1=(j for i in range(5))
will yield five 4’s when iterated. Why it will not yield 01234 like when we replace j with i.
-
How this statement working?
a = [0, 1, 2, 3] for a[-1] in a: print(a[-1]) 0 1 2 2
Your first example is a generator expression (the parentheses are required, so the first version you show doesn’t actually make sense). The generator expression repeatedly yields j
, which is not defined in the code you show. But from your description, it already has the value 4
in the environment you were testing in, so you see that value repeatedly. You never use the i
value, which is what’s getting the values from the range
.
As for your other loop, for a[-1] in a
keeps rebinding the last value in the list, and then printing it.
A for loop does an assignment to the target you give it between for
and in
. Usually you use a local variable name (like i
or j
), but you can use a more complicated assignment target, like self.index
or, as in this case, a[-1]
. It’s very strange to be rewriting a value of your list as you iterate over it, but it’s not forbidden.
You never get 3
printed out, because each of the previous assignments as you iterated overwrote it in the list. The last iteration doesn’t change the value that gets printed, since you’re assigning a[-1]
to itself.
Your first question raises error when iterating, as j
is not defined. If you are getting any response from that, you probably have defined a value for j
above your generator, which sounds to be 4
in your code.
About your second question:
when you have something like this:
for i in a:
...
Actually you are are doing this in each cycle: i=a[0], i=a[1], ...
.
When you write a[-1] in a
, it is kind of equal to: a[-1]=a[0], a[-1]=a[1], ...
and you are changing last element of a
each time. So at the end, the last element of your list would be 2
.