I'm doing this in attempt to better understand the iter() and next() methods better.
So I understand this can easily be done like this using some built-ins:
>>>animal = 'cat'
>>>print(list(reversed(animal)))
['t','a','c']
But this can also be done creating a class iterable:
class Reverselist():
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise StopIteration
self.index =self.index -1
return self.data[self.index]
def intolist(self):
self.test = [i for i in self]
return self.test
rev = Reverselist('cat')
print(rev.intolist())
['t','a','c']
However, I'm having some trouble doing the same thing but using functions instead of going the OOO route.
Is there a way to do this with functions using iter() and next() methods without having to resort to comprehensions and loops?
while index > 0or afor index in range(len(lst)-1,-1,-1)).