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 374213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:23:02+00:00 2026-05-12T14:23:02+00:00

I am probably going about this in the wrong manner, but I was wondering

  • 0

I am probably going about this in the wrong manner, but I was wondering how to handle this in python.

First some c code:

int i;

for(i=0;i<100;i++){
  if(i == 50)
    i = i + 10;
  printf("%i\n", i);
}

Ok so we never see the 50’s…

My question is, how can I do something similar in python? For instance:

for line in cdata.split('\n'):
  if exp.match(line):
    #increment the position of the iterator by 5?
    pass
  print line

With my limited experience in python, I only have one solution, introduce a counter and another if statement. break the loop until the counter reaches 5 after exp.match(line) is true.

There has got to be a better way to do this, hopefully one that does not involve importing another module.

Thanks in advance!

  • 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-12T14:23:02+00:00Added an answer on May 12, 2026 at 2:23 pm

    There is a fantastic package in Python called itertools.

    But before I get into that, it’d serve well to explain how the iteration protocol is implemented in Python. When you want to provide iteration over your container, you specify the __iter__() class method that provides an iterator type. “Understanding Python’s ‘for’ statement” is a nice article covering how the for-in statement actually works in Python and provides a nice overview on how the iterator types work.

    Take a look at the following:

    >>> sequence = [1, 2, 3, 4, 5]
    >>> iterator = sequence.__iter__()
    >>> iterator.next()
    1
    >>> iterator.next()
    2
    >>> for number in iterator:
        print number 
    3
    4
    5
    

    Now back to itertools. The package contains functions for various iteration purposes. If you ever need to do special sequencing, this is the first place to look into.

    At the bottom you can find the Recipes section that contain recipes for creating an extended toolset using the existing itertools as building blocks.

    And there’s an interesting function that does exactly what you need:

    def consume(iterator, n):
        '''Advance the iterator n-steps ahead. If n is none, consume entirely.'''
        collections.deque(itertools.islice(iterator, n), maxlen=0)
    

    Here’s a quick, readable, example on how it works (Python 2.5):

    >>> import itertools, collections
    >>> def consume(iterator, n):
        collections.deque(itertools.islice(iterator, n))
    >>> iterator = range(1, 16).__iter__()
    >>> for number in iterator:
        if (number == 5):
            # Disregard 6, 7, 8, 9 (5 doesn't get printed just as well)
            consume(iterator, 4)
        else:
            print number
    
    1
    2
    3
    4
    10
    11
    12
    13
    14
    15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am probably going about this all wrong, but here's what I'm trying to
I'm probably going about this all wrong, but hey. I am rendering a large
Okay, so maybr I'm going about doing this entirely wrong, I probably am. But
I'm probably going about this the wrong way but... I’ve added a field to
I know I am probably going about this the wrong way, but I am
I'm probably going about this the wrong way, but here goes: I am starting
I'm probably going about this all wrong but... I am trying to populate a
I'm probably missing something obvious or going about this in the wrong way, but
I'm probably going about this the wrong way, please feel free to correct me.
New to programming in general, so I'm probably going about this the wrong way.

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.