Is there an iterator to reverse a list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there an iterator to reverse a list?

Other than manually looping through a list, is there a way to get an iterator that goes from the end to the start. If so, I would like to use this with a predicate function and return the first matching element. I have done this with a forward search but I could not find a built-in way to reverse the search direction. Does this exist or do I have to creat my own iterator?

31st Dec 2016, 2:18 PM
Nathan
Nathan - avatar
2 Answers
+ 5
your_list[::-1] eg: li = [1,2,3,4,5,6,7,8,9] print(li[::-1]) #outputs [9,8,7,6,5,4,3,2,1] Is this what you're asking for?
8th Feb 2017, 2:12 PM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
+ 2
You can use the reversed function for this.
31st Dec 2016, 3:29 PM
James Durand
James Durand - avatar