I have a code that print something like:
N, M = map(int,input().split())
x='.|.'
j=1
for i in range(N):
print((x*j).center(M,'-'))
j =2
I have tried to shorten the code by using list comprehension:
[print((x*j).center(M,'-')) for i in range(N)]
How can I update the variable j for each loop? Thanks.
CodePudding user response:
I have worked around the problem using this:
[print((x*(1 i*2)).center(M,'-')) for i in range(N)]
This solution maybe short but I think it's not ideal for developing.
CodePudding user response:
Why use i in a for loop when you could just use j?
for j in range(1, N, 2):
print( (x*j).center(M, '-') )
This is sufficiently short, and consistent with the comments which correctly state that a list comprehension is used to build a list in a more condensed format, rather than be used whenever possible to use one less line of code.
