4 Replies - 487 Views - Last Post: 09 January 2012 - 06:13 AM Rate Topic: -----

Topic Sponsor:

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 520
  • Joined: 25-November 10

Bizarre behaviour while iterating list

Posted 09 January 2012 - 05:28 AM

Hi, I want to iterate over a list, printing each item and subsequently deleting it, so that at the end, the list is empty, while all the numbers are printed out. I have tried two tactics (to me the logic for both is the same, just checked to spot the problem), but in either case, neither is the list fully empty after the iteration, nor are all the numbers printed. Can somebody tell me what is the problem, and how can I implement what I am trying to do?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Bizarre behaviour while iterating list

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1756
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: Bizarre behaviour while iterating list

Posted 09 January 2012 - 05:42 AM

The problem is that you are deleting the elements after printing them, which changes the array by moving each element one index down, while the index is still incremented as if it were dealing with the original array.

I mean, this is basically what is happening:
# The first iteration
a = [1, 2, 3]
index = 0 # Prints 1

# The second iteration
a = [2, 3]
index = 1 # Prints 3

# The third iteration
a = [2]
index = 2 # Index is outside the range of the array



What you should be doing is either printing each element of the array in a loop, and then clearing the array once all of them have been printed; or printing and deleting always the first index of the array while the array has elements. Something like:
while len(a) > 0:
    print a[0]
    del a[0]


This post has been edited by Atli: 09 January 2012 - 06:15 AM
Reason for edit:: Fixed the array indexes. (See cupidvogel's next post.)

Was This Post Helpful? 2
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 520
  • Joined: 25-November 10

Re: Bizarre behaviour while iterating list

Posted 09 January 2012 - 05:54 AM

I guess you meant a[0], not a[1], right?, for in Python, just like in C, and unlike, say, Matlab, the starting index is 0 and not 1.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1756
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: Bizarre behaviour while iterating list

Posted 09 January 2012 - 06:12 AM

O yea, that's right. Sorry. Been reading a lot of LUA code lately. :)
Was This Post Helpful? 0
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 520
  • Joined: 25-November 10

Re: Bizarre behaviour while iterating list

Posted 09 January 2012 - 06:13 AM

:bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1