Pythonic way of splitting loop over list in two parts with one iterator

Chiel

I am processing a text file with an irregular structure that consists of a header and of data in different sections. What I aim to do is walk through a list and jump to the next section once a certain character is encountered. I made a simple example below. What is the elegant way of dealing with this problem?

lines = ['a','b','c','$', 1, 2, 3]

for line in lines:
    if line == '$':
        print("FOUND END OF HEADER")
        break
    else:
        print("Reading letters")

# Here, I start again, but I would like to continue with the actual
# state of the iterator, in order to only read the remaining elements.
for line in lines:
    print("Reading numbers")
Olivier Melançon

You actually can have one iterator for both loops by creating your line iterator outside the for loop with the builtin function iter. This way it will be partially exhausted in the first loop and reusable in the next loop.

lines = ['a','b','c','$', 1, 2, 3]

iter_lines = iter(lines) # This creates and iterator on lines

for line in iter_lines :
    if line == '$':
        print("FOUND END OF HEADER")
        break
    else:
        print("Reading letters")

for line in iter_lines:
    print("Reading numbers")

The above prints this result.

Reading letters
Reading letters
Reading letters
FOUND END OF HEADER
Reading numbers
Reading numbers
Reading numbers

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Splitting the list in to two parts in Scala

From Dev

Pythonic way to loop over dictionary

From Dev

Pythonic way to unpack an iterator inside of a list

From Java

pythonic way to iterate over part of a list

From Java

pythonic way to iterate over part of a list

From Dev

pythonic way to vectorize ifelse over list

From Dev

Pythonic way to iterate over a shifted list of tuples

From Dev

Iterate over list of class objects - 'pythonic' way

From Dev

Is there a Pythonic way to iterate over an "expanded" source list?

From Dev

pythonic way of splitting a list of lists into a length-keyed dictionary?

From Dev

Splitting a string in two parts

From Dev

Splitting an array into two parts

From Dev

Pythonic way of handling two statements in a for loop?

From Dev

Pythonic way for Zigzag Iterator?

From Dev

Loop search and replace two-part string over file using PowerShell while preserving one of the parts

From Dev

Is there a more pythonic way of writing a loop that modifies a list?

From Dev

Is there a way of splitting a list of coordinates into separate parts, based on repeating values?

From Dev

Pythonic way for returning elements of one list by an order-preserved comparation of two other lists

From Dev

Is there a prefered way of splitting a list into two based on indexes

From Java

What is the most "pythonic" way to iterate over a list in chunks?

From Dev

Pythonic way to iterate over list and create a tuple with each value

From Dev

What is the most pythonic way to iterate over a list until a condition is met?

From Dev

Pythonic way of looping over variable that is either an element or a list

From Dev

Pythonic way to check if two list of list of arrays are equal

From Dev

What is the most pythonic way to map two different strings, to act as one?

From Dev

Pythonic way of replace values in one column from a two column table

From Dev

Pythonic way to get either one of two attributes from a Python object

From Dev

Javascript: Nicest way to catch a for loop over an undefined/null iterator?

From Dev

Pythonic way to get index of items where two list intersect

Related Related

  1. 1

    Splitting the list in to two parts in Scala

  2. 2

    Pythonic way to loop over dictionary

  3. 3

    Pythonic way to unpack an iterator inside of a list

  4. 4

    pythonic way to iterate over part of a list

  5. 5

    pythonic way to iterate over part of a list

  6. 6

    pythonic way to vectorize ifelse over list

  7. 7

    Pythonic way to iterate over a shifted list of tuples

  8. 8

    Iterate over list of class objects - 'pythonic' way

  9. 9

    Is there a Pythonic way to iterate over an "expanded" source list?

  10. 10

    pythonic way of splitting a list of lists into a length-keyed dictionary?

  11. 11

    Splitting a string in two parts

  12. 12

    Splitting an array into two parts

  13. 13

    Pythonic way of handling two statements in a for loop?

  14. 14

    Pythonic way for Zigzag Iterator?

  15. 15

    Loop search and replace two-part string over file using PowerShell while preserving one of the parts

  16. 16

    Is there a more pythonic way of writing a loop that modifies a list?

  17. 17

    Is there a way of splitting a list of coordinates into separate parts, based on repeating values?

  18. 18

    Pythonic way for returning elements of one list by an order-preserved comparation of two other lists

  19. 19

    Is there a prefered way of splitting a list into two based on indexes

  20. 20

    What is the most "pythonic" way to iterate over a list in chunks?

  21. 21

    Pythonic way to iterate over list and create a tuple with each value

  22. 22

    What is the most pythonic way to iterate over a list until a condition is met?

  23. 23

    Pythonic way of looping over variable that is either an element or a list

  24. 24

    Pythonic way to check if two list of list of arrays are equal

  25. 25

    What is the most pythonic way to map two different strings, to act as one?

  26. 26

    Pythonic way of replace values in one column from a two column table

  27. 27

    Pythonic way to get either one of two attributes from a Python object

  28. 28

    Javascript: Nicest way to catch a for loop over an undefined/null iterator?

  29. 29

    Pythonic way to get index of items where two list intersect

HotTag

Archive