0

i got this list of objects with one item in it.

    print self.parameters
    print len(self.parameters)
    for p in self.parameters:
        print p

when i print out the list and len of the lsit i see the expected: one item. But when looping over the lsit i also get a None item...!?

[<__main__.Parameter object at 0x00000000022D4828>]
1
<__main__.Parameter object at 0x00000000022D4828>
None

what is going on here? (yes im sure, that the "None" output is from this print statement)


EDIT: i was manipulating the list i was looping over:

print self.parameters
print len(self.parameters)
for p in self.parameters:
    print p
    (...)
    self.parameters.append(<something that returned None>)
Dill
  • 1,943
  • 4
  • 20
  • 28

1 Answers1

1

Don't do this.

for p in self.parameters:
    print p
    ...
    self.parameters.append(...) # No.

See Modifying list while iterating -- basically, you shouldn't modify something while iterating over it. You can make a copy if you want:

for p in list(self.parameters):
    print p
    ...
    self.parameters.append(...) # Okay
Community
  • 1
  • 1
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415