Jakub Turcovsky
Jakub Turcovsky

Reputation: 2126

Going through list from the end after split()

for item in "1,2,3,4,5,6".split(","):

I need to reverse the list to have first 6, then 5... Does an idea come to your mind how to do it more effectively than reverse it first and then use it? Some function I don't know yet?

Upvotes: 0

Views: 56

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121564

Use the reversed() function:

for item in reversed("1,2,3,4,5,6".split(",")):

Demo:

>>> for item in reversed("1,2,3,4,5,6".split(",")):
...     print item
... 
6
5
4
3
2
1

Upvotes: 5

Related Questions