0

I have a python script that does the following task. It does a for loop to go through the array items and gives relevant information belonging to that item.

I want to update the array in real-time and also the for loop must be executed without resetting. Some ML training happens inside the for loop so it should not be restarted whenever the array get updated. At the same time the for loop must be able to detect the appended items in the array.

'''

arr = [a,b,c]
for i in arr:
    print(i)     #and do something

Now basically the arr will be appended by listening to a DB column. For instance, i do

arr.append(dbData)

I know I can use the function to call the for loop again, but how to do it without having to reset the loop?

Thank you for your help. Kindly let me know if the question is unclear.

Vik
  • 59
  • 7
  • Does this answer your question? [Python: Adding element to list while iterating](https://stackoverflow.com/questions/3752618/python-adding-element-to-list-while-iterating) – Martheen Jan 05 '21 at 15:15

2 Answers2

0

See also here

You create an iterator over the list and add to it as needed

arr = [a,b,c]
for i in arr:
    print(i) 

islice(arr,0,len(arr)-1)

Check out this tutorial for a good intro on how to use the islice method

Rethipher
  • 336
  • 1
  • 14
0

I am not kind of sure about islice iterator. The following code gave me the right outputs.

myarr = [1,2,3]
bb = []
while True:
    for a in myarr:
        if len(myarr)==3: #some condition
            bb.append(4)
            myarr.extend(bb)
        print(a, myarr)

Thanks

Vik
  • 59
  • 7