-1

I run this code in a loop with different length of father[0] value each time. All works well but sometimes it pops the error "String index out of range"

for a in range(len(father[0])):

        column = [item[a] for item in father]
H_vinx
  • 1
  • 1
  • 2
    I guess it's because not all elements of father are of the same length, more precisely you have shorter elements than the first element. – palvarez Aug 07 '19 at 08:18
  • Maybe you are looking to flatten a list? Check out this if that is the case https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists – palvarez Aug 07 '19 at 08:20
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Aug 07 '19 at 08:22

2 Answers2

0

Just put a checker for empty string before you jump into the loop. Mind empty strings as they may throw the exception you're currently facing.

>>> empty_string = ''
>>> empty_string[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
null
  • 1,944
  • 1
  • 14
  • 24
0

You might want to see, what values you are passing to father[0], as mentioned by @Devrim above it could be the string is empty.