Is there a way to remember the position in a python iterator? -


i iterate on iterable object (let's say, list) , leave @ point remembering position left off continue next time iterator object called.

something like:

for val in list:    do_stuff(val)    if some_condition:        break  do_stuff()  val in list:     continue_doing_stuff(val) 

speed matters , list considered quite large. saving object , iterating again through whole list until saved element found not option. possible without writing explicit iterator class list?

as peter suggested in comments, can wrap iterable in iter() make generator:

a_list = ['a', 'b', 'c', 'd'] iter_list = iter(a_list)  val in iter_list:    print(val) # do_stuff(val)    if val == 'b':        break  print('taking break') # do_stuff()  val in iter_list:     print(val) #continue_doing_stuff(val) 

shows:

a b taking break c d 

and think elegant approach.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -