4

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
  • 345
  • 1
  • 6
  • 18

2 Answers2

6

Try:

list = [4,3,2,1]

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

Output:

[1, 2, 3, 4]
Andrés Pérez-Albela H.
  • 3,873
  • 1
  • 16
  • 28
1
list = [4,3,2,1]

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

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

Inkblot
  • 584
  • 2
  • 7
  • 17