this is the result I'm getting
I don't get this result with the code shown, although it would make sense if t
were initially -1
rather than 0
(might make sense as something you were temporarily testing).
Why is the script not iterating for a fourth time?
Because the loop iterates for i in tableData
, and there are only three lists in tableData
.
The condition t <= (len(i))
does not make any sense. i
is one of the lists, so len(i)
is a bound on indexes into that list. But t
does not function as an index into a sublist; it functions as an index into the list of lists - because it is incremented by a loop over the list of lists.
what does it say about looping though list of lists in python?
That it works exactly the same way as looping through a list of anything else in Python.
Each time through the list, i
is one of the elements of tableData
. In this case, those elements are lists.
You apparently want to combine elements from the separate lists, according to their position. In other words, you want to iterate over the lists in parallel, which is done with zip
. I will close the question as a duplicate after providing the deeper explanation here, since that is the proper way to solve the problem.
But let's first debug the attempted approach.
We presume that each list in the list of lists has the same length. We want to iterate over one of those lists, in order to have the appropriate amount of iterations. Then we need an index each time through the loop, so that we can use it for each iteration. That's what t
was doing in the original code. Here, we'll do it using enumerate
, which is the normal tool (I'll show you another duplicate for that).
We will take away the useless and wrong if
condition, and ignore the element that is returned from enumerate
to use only the index. We will take away manual incrementing of the index, since enumerate
is already taking care of it.
Finally, we can use modern string formatting to assemble the data.
That gives us:
for i, _ in enumerate(tableData[0]):
print(f"{tableData[0][i]:>8} {tableData[1][i]:>5} {tableData[2][i]:>5}")
As an aside: please drop the habit of using i
as a variable name for for
loops. Remember that Python's for
loop gives you an element of the original sequence, and not a numeric index. The name i
wrongly suggests the latter.