Reputation: 1
arr = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
arr[i - 1] = arr[i]
for i in range(0, 6):
print(arr[i], end = " ")
Answer is :2 3 4 5 6 6
Upvotes: 0
Views: 76
Reputation: 21
if you are refering to get the following result : 2 3 4 5 6 1 then you should rearrange the following way:
for i in range(0, 6):
arr[i-1] = arr[i]
that way, you start with arr[-1] = arr[0], so 6 will be replaced with 1 and so the code goes on
Upvotes: 0
Reputation: 27105
This is your code:
arr = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
arr[i - 1] = arr[i]
for i in range(0, 6):
print(arr[i], end = " ")
arr is a list comprised of 6 integers.
In your first for loop, i will vary from 1 to 5 (inclusive).
Python lists are accessed using base-0 - i.e., the first element is at index zero, the second at 1 and so on.
Therefore your loop is moving every element to the position (index) that immediately precedes it. Nothing is moved into index 5
Upvotes: 1