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]
Try:
list = [4,3,2,1]
print [x for x in list[::-1]]
Output:
[1, 2, 3, 4]
list = [4,3,2,1]
print [i for i in list[::-1]]
Which gives the desired [1, 2, 3, 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With