11

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]

1
  • 3
    for i in list_[::-1]: Commented Nov 25, 2015 at 16:21

2 Answers 2

14

Try:

list = [4,3,2,1]

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

Output:

[1, 2, 3, 4]
Sign up to request clarification or add additional context in comments.

Comments

4
list = [4,3,2,1]

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

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

2 Comments

Nice answer, same as mine.
Ah, you just beat me to it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.