Bharath K
Bharath K

Reputation: 197

Traversing python list

I have 500 records in my database and I am loading all of them into a python list of dictionary elements.(25 key value pairs)

I am calling a method that traverses all the dictionary elements in the list and performs some action on each record. While traversing, I have noticed that after traversing half of the elements, logic is exiting the method and restarting again for the next half.

I tried to put it as a example. (I can't copy the exact code as it is not possible)

Ex:

def loadrec():
  reading from database and appending rows to a global list(mylist)

def myrun():
  print "****** Execution Started **********"
  for row in mylist:
    doing some operations and printing a string

if __name__=="__main__":
i=0
while (i>=0): #This never ends(to make the script run forever)
   loadrec()
   myrun()
   print "****** Execution Ended ************"



Result:


****** Execution Started **********
printed 250 records
****** Execution Ended ************


****** Execution Started **********
printed 125 records
****** Execution Ended ************


****** Execution Started **********
printed 62 records
****** Execution Ended ************

****** Execution Started **********
printed 31 records
****** Execution Ended ************

****** Execution Started **********
printed 15 records
****** Execution Ended ************

****** Execution Started **********
printed 7 records
****** Execution Ended ************

****** Execution Started **********
printed 1 record
****** Execution Ended ************

I am not sure why it is processing only half of total records every time. But, at the end, it is processing all the records.

I have tried checking if there is any issue with python list maximum size or memory, but none of them seem to be a possible scenario.

Would be glad to know any hint of what could have been the reason.

Upvotes: 0

Views: 65

Answers (1)

Andrew Guy
Andrew Guy

Reputation: 9968

I'm going to take a wild stab in the dark here.

You are doing something like this:

def myrun():
    print "****** Execution Started **********"
    for row in mylist:
        #Some processing here
        myList.remove(row)

You cannot mutate a list that you are iterating over. This would account for only processing half the list each time. You will need to refactor your code. Without you showing us your code, I'm not even going to try and do the refactor.

Upvotes: 1

Related Questions