9

I have a list of 1000 elements. How do I access all of them starting from last element to the first one in a loop.

list = [4,3,2,1]
for item in list:
    print(from last to first)

output = [1,2,3,4]

Rakesh Nittur
  • 445
  • 1
  • 7
  • 20

2 Answers2

12

Try:

list = [4,3,2,1]

print [x for x in list[::-1]]

Output:

[1, 2, 3, 4]
Andrés Pérez-Albela H.
  • 4,003
  • 1
  • 18
  • 29
3
list = [4,3,2,1]

print [i for i in list[::-1]]

Which gives the desired [1, 2, 3, 4]

Inkblot
  • 708
  • 2
  • 8
  • 19