2

I have a simple function with the following

comdList = range(0,27)
for t, in comdList:
    print t

However it returns a in object not iterable error

outside the function it works fine. Whats going on??

Óscar López
  • 232,561
  • 37
  • 312
  • 386
Darran
  • 23
  • 1
  • 6

1 Answers1

6

Try this:

for t in comdList:
    print t

The extra comma after the t variable was causing the error, because of it Python thinks that the iterable is going to return a sequence of 1-tuples to unpack - for example: ((1,), (2,)) but instead it received an iterable of single elements.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • @ Óscar López - oh the comma... duh it it always the most obvious things that you miss thanks – Darran Jun 21 '13 at 01:27
  • 2
    @Darran you're welcome! please don't forget to [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) this or any other answer that you find helpful, just click on the check mark to its left ;) – Óscar López Jun 21 '13 at 01:28
  • Actually, `for t, in comdList` would only work if `comdList` was a sequence of 1-tuples ( `(1,), (2,), ...` ), not a sequence of higher-dimension tuples. – chepner Jun 21 '13 at 03:26