Meaning of A[i] in "list in range"

Hi. i need som help to understand this code. What does A[i] and A[L-1] mean? Why write it like this?

def myst(A):
      L=len(A) -1
      for i in range (len (A) // 2):
            t = A[i]
            A[i] = A[L-i]
            A[L-i] = t
      return A 

Welcome to the community of freecodecamp forum @kims1 ! :partying_face:

List[x] is the syntax to identify the x-th element inside List
The algorithm you posted is used to reverse the list you pass to the function^^

Just to make an example: the first time you enter the loop i is equal to 0.
A[i] is equal to A[0] which is 1 ( the first element of the list [1,2,3,4,5,6])
A[L-i] is equal to A[5] which is 6 ( the last element of the list [1,2,3,4,5,6])

To supplement the answer provided above, probably what you need to understand is that list indexing starts from 0. e.g.
mylist = [2, 6, 10, 12 ]
The index of the first element is 0, second element is 1, third element is 2 e.t.c.
Though the length of the list is 4, the index of the last element is 3, less than the length of the list by 1. To access the first element of the list you use list indexing like this: mylist[0] gets the first element 2, second element 6 is got using mylist[1] e.t.c.
I hope you get the point.

1 Like