Why can't I iterate twice over the same data?

JSchwartz :

Honestly I am a little confused here, why can't I iterate twice over the same data?

def _view(self,dbName):
    db = self.dictDatabases[dbName]
    data = db[3]

    for row in data:
        print("doing this one time")

    for row in data:
        print("doing this two times")

This will print out "doing this one time" a few times (as data has a few rows), however it will NOT print out "doing this two times" at all ...

The first time I iterate over data works fine, but the second time when I run the last list "for row in data" this returns nothing ... so executing it one time works but not twice ... ?

FYI - data is a csv.reader object (in case that is the reason)...

Óscar López :

It's because data is an iterator, an you can consume an iterator only once. For example:

lst = [1, 2, 3]
it = iter(lst)

next(it)
=> 1
next(it)
=> 2
next(it)
=> 3
next(it)
=> StopIteration

If we are traversing some data using a for loop, that last StopIteration will cause it to exit the first time. If we try to iterate over it again, we'll keep getting the StopIteration exception, because the iterator has already been consumed.

Now for the second question: What if we do need to traverse the iterator more than once? A simple solution would be to create a list with the elements, and we can traverse it as many times as needed. This is all right as long as there are few elements in the list:

data = list(db[3])

But if there are many elements, it's a better idea to create independent iterators using tee():

import itertools
it1, it2 = itertools.tee(db[3], n=2) # create as many as needed

Now we can loop over each one in turn:

for e in it1:
    print("doing this one time")

for e in it2:
    print("doing this two times")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't I iterate twice over the same data?

Java: why can't iterate over an iterator?

Why same Go Channel can't be started twice?

Why can I iterate over a slice twice, but not a vector?

Why I can't reuse WebClient to make the same request twice?

Why can't write internal flash of same address twice

Why can't you increment/decrement a variable twice in the same expression?

Why can't I loop over $q->fetch() twice?

Why I can't change a bool twice?

Why can't I iterate over a list of Firebase Img URLS, but can access each on individually?

How can I iterate over each data label of each series

Can't iterate over 2d array to normalize data

Why can't I use the same Get-Date variable twice without getting incorrect time

Why can't i receive twice from the same client

Why can't I iterate over locals() and within the iteration use returned item as a key?

Why can’t I insert an entity twice by using Spring Data Jdbc?

How can I iterate over all views known to the data binder?

Why can't I iterate over a Counter in Python?

Why I can't iterate over a binary multiple line reading?

Why can't I iterate over this array?

How can I iterate over unordered JSON data and sort into an array

Why can't I iterate over this array saved on an html data selector with javascript?

Why can't I use the same method twice for one instance in ruby?

Is there a way i can iterate over this list without repeating the same number?

why can't I pass the return twice?

Why can't I Iterate over a string that is returned in C?

Why can't I use the same parser twice in a tuple()?

Why can't i use same file channel twice in TCL

I can't iterate over a javascript array