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
One of the most basic challenges in python is getting clever when iterating over
lists anddicts. 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.
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.