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

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are calling the asynchronous $.get() method, where its callback… May 16, 2026 at 12:13 pm
  • Editorial Team
    Editorial Team added an answer You'll need to put the directory in the URL you… May 16, 2026 at 12:13 pm
  • Editorial Team
    Editorial Team added an answer Which variable are you using for the database input? If… May 16, 2026 at 12:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there any way to make a list of classes behave like a set
Is there anyway to encorporate Dragon NaturallySpeaking into an event driven program? My boss
I have a server-code that's written in Python, and I have a client-code that's
I am not sure where i've seen this before, but im sure that there
I have an application that's locked down using forms authentication. There are few nightly
I wrote a quick program in python to add a gtk GUI to a
I'm interested in experimenting with embedding Python in my application, to let the user
I have been doing some functional programming and had a question. Perhaps I might
I am using Visual Studio 2008 C++ project (Visa 32 bit). I have the
I need to do a rounding like this and convert it as a character:

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.