5

Can someone please explain the following Python code?

a = [1, 2, 3, 4]
for a[-1] in a:
 print(a)

And after execution, I am getting the following result but I am unable to understand the whole logic behind it.

Result

[1, 2, 3, 1]
[1, 2, 3, 2]
[1, 2, 3, 3]
[1, 2, 3, 3]
asked Jul 8, 2022 at 6:54
2
  • 1
    a[-1] is the last value in the array so you are assigning every value in the array to the last value 1 by 1. However, you never get to 4 since 4 has been overwritten before you iterate that far. Commented Jul 8, 2022 at 6:57
  • Out of curiosity, where did you find this code? Commented Jul 8, 2022 at 7:04

2 Answers 2

2

This loop will visit each slot of the list and put its value in a[-1], which is the last slot of the list. In this case a[-1] is a reference to the same slot as a[3].

So we can unwrap the loop to this:

a = [1, 2, 3, 4]
a[3] = a[0] # First iteration
print(a)
a[3] = a[1] # Second iteration
print(a)
a[3] = a[2] # Third iteration
print(a)
a[3] = a[3] # Last iteration. Nothing changes here
print(a)
answered Jul 8, 2022 at 7:00
Sign up to request clarification or add additional context in comments.

3 Comments

So the in keyword can do assignment in a list? That feels weird
It is of course not what a good coder would do, and I suppose this is just a fabricated exercise. But it is not specific to Python. For example, in JavaScript it would be for (a[a.length-1] of a) console.log(a);. Same effect.
It is consistent behavior, the whole purpose of in is for assignment. It also makes sense to have it keep the general rules of assignments. But it's still a combination I would not have thought of. In that way it'S a pretty great question.
1

Basically in each iteration, the index position value is replaced in list. Let me run through a simple example first then to your question.

Example:

a = [1, 2, 3, 4]
for a[0] in a:
 print(a[0])
 print(a) # It replaces the first position a[0] with each iteration a[0, 1, 2, 3]

Output:

1 => (iteration 1, a[0]=1, replaced with 1 in a[0] position)
[1, 2, 3, 4]
2 => (iteration 2, a[0]=2, replaced with 2 in a[0] position)
[2, 2, 3, 4]
3 => (iteration 3, a[0]=3, replaced with 3 in a[0] position)
[3, 2, 3, 4]
4
[4, 2, 3, 4] => (iteration 4, a[0]=4, replaced with 4 in a[0] position)

Explanation on your Query:

a = [1, 2, 3, 4]
for a[-1] in a:
 print(a[-1])
 print(a) # It replaces the last position a[-1] with each iteration a[0, 1 ,2, 3]

Output:

1 => (iteration 1, a[-1]=1, replaced with 1 in a[-1] position)
[1, 2, 3, 1]
2 => (iteration 2, a[-1]=2, replaced with 2 in a[-1] position)
[1, 2, 3, 2]
3 => (iteration 3, a[-1]=3, replaced with 3 in a[-1] position)
[1, 2, 3, 3]
3 => (iteration 4, a[-1]=3 (as the position is already 
[1, 2, 3, 3] replaced with 3. So, placed with 3 in a[-1] position))
answered Jul 8, 2022 at 7:58

1 Comment

Excellent demonstration, especially the last iteration with 3 instead original 4 on the last element was confusing before you explained it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.