Reputation: 437
I have the a program to print items from lists within lists in a specific way. Here is that piece of code:
for y in range(0,5):
print '\n'
for x in tableau:
if y < len(x):
print x[y],
else :
print ' '
What I want is for the if statement go back to the inner loop(for x in tables) after it executes the print ' ' in the else part of if statement. Is there any way to do that?
Upvotes: 0
Views: 1718
Reputation: 816
As suggested by Delgan, the answer is staightforward - you have to use the break
keyword :
for y in range(0,5):
print '\n'
for x in tableau:
if y < len(x):
print x[y],
else :
print ' '
break
The break keyword exits the most inner loop.
Upvotes: 1
Reputation: 53
for y in range(0,5):
print '\n'
for x in tableau:
if y < len(x):
print x[y],
else :
print ' '
break
Will break out of the inner for and back into the outer for, after the print is executed. This will print a "\n" and then move back to the inner for, which I believe is what you are asking?
Upvotes: 1
Reputation: 571
As I don't really understand what you need, I suggest you 2 solutions
First one: print ' ' instead of inexistent list element (3 times if len(x) == 2)
for x in tableau:
print '\n'
for y in x:
print y
for y in range(5 - len(x))
print ' '
Second one: print ' ' always at the end of the list
for x in tableau:
print '\n'
for y in x:
print y
print ' '
Upvotes: 1