Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 767365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:01:04+00:00 2026-05-14T18:01:04+00:00

Is there anyway to make a python list iterator to go backwards? Basically i

  • 0

Is there anyway to make a python list iterator to go backwards?

Basically i have this

class IterTest(object):
    def __init__(self, data):
        self.data = data
        self.__iter = None

    def all(self):
        self.__iter = iter(self.data)
        for each in self.__iter:
            mtd = getattr(self, type(each).__name__)
            mtd(each)

    def str(self, item):
        print item

        next = self.__iter.next()
        while isinstance(next, int):
            print next
            next = self.__iter.next()

    def int(self, item):
        print "Crap i skipped C"

if __name__ == '__main__':
    test = IterTest(['a', 1, 2,3,'c', 17])
    test.all()

Running this code results in the output:

a
1
2
3
Crap i skipped C

I know why it gives me the output, however is there a way i can step backwards in the str() method, by one step?

EDIT

Okay maybe to make this more clear. I don’t want to do a full reverse, basically what i want to know if there is an easy way to do the equivalent of a bidirectional iterator in python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:01:05+00:00Added an answer on May 14, 2026 at 6:01 pm

    No, in general you cannot make a Python iterator go backwards. However, if you only want to step back once, you can try something like this:

    def str(self, item):
        print item
    
        prev, current = None, self.__iter.next()
        while isinstance(current, int):
            print current
            prev, current = current, self.__iter.next()
    

    You can then access the previous element any time in prev.

    If you really need a bidirectional iterator, you can implement one yourself, but it’s likely to introduce even more overhead than the solution above:

    class bidirectional_iterator(object):
        def __init__(self, collection):
            self.collection = collection
            self.index = 0
    
        def next(self):
            try:
                result = self.collection[self.index]
                self.index += 1
            except IndexError:
                raise StopIteration
            return result
    
        def prev(self):
            self.index -= 1
            if self.index < 0:
                raise StopIteration
            return self.collection[self.index]
    
        def __iter__(self):
            return self
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a Fenwick tree class in python. Basically I have an internal list
Is there anyway to make a secondary key in SQL? Lets say I have
Is there any way to make this work in Java? public static void change(List<?
In Python is there any way to make a class, then make a second
Is there any way to translate this Java code into Python? class Foo {
echo ddayaynightday | sed 's/day//g' It ends up daynight Is there anyway to make
is there anyway in QTP to make changes take place on all function libraries
Is there any way to make nice round corners with python? Currently PIL and
The problem I have is that I have this Python script to launch a
Is there any way to make the computer a Python program is running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.