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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:19:17+00:00 2026-05-24T04:19:17+00:00

Is there a pythonic way (I know I can loop using range(len(..)) and get

  • 0

Is there a pythonic way (I know I can loop using range(len(..)) and get an index) to do the following example:

for line in list_of_strings:
    if line[0] == '$':
        while line[-1] == '#':
            # read in the Next line and do stuff
        # after quitting out of the while loop, the next iteration of the for loop
        # should not get any of the lines that the while loop already dealt with

essentially, the nested while loop should be incrementing the for loop.

Edit: Not a file handle, confused two things I was working on, it’s a list of strings

  • 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-24T04:19:18+00:00Added an answer on May 24, 2026 at 4:19 am

    One of the most basic challenges in python is getting clever when iterating over lists and dicts. If you actually need to modify the collection while iterating, you may need to work on a copy, or store changes to apply at the end of iteration.

    In your case, though, you just need to skip items in the list. You can probably manage this by expanding iteration into an more explicit form.

    i = iter(list_of_strings)
    for line in i:
        if line.startswith('$'):
            while line.endswith('#'):
                line = i.next()
            # do work on 'line' which begins with $ and doesn't end with #
    

    and that’s all you really need to do.

    Edit: As kindall mentions, if the while portion may iterate past the end of the input sequence, you’ll need to do a bit more. You can do the following.

    i = iter(list_of_strings)
    for line in i:
        if line[0] == '$':
            try:
                while line[-1] == '#':
                    line = i.next()
            except StopIteration:
                break
            # do work on 'line' which begins with $ and doesn't end with #
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty sure there should be a more Pythonic way of doing this -
There are a few ways to get class-like behavior in javascript, the most common
What is the best (or 'Pythonic') way to test if a class has a
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking
There seem to be many ways to define singletons in Python. Is there a
There are numerous Agile software development methods. Which ones have you used in practice
There are several types of objects in a system, and each has it's own

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.