# Quick Question (for-loops)

I’m new to python and I was trying to find a more elegant way of to code this.

``````if len(kolom) == 1:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]])
if len(kolom) == 2:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]])
if len(kolom) == 3:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]])
if len(kolom) == 4:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]])
if len(kolom) == 5:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]])
if len(kolom) == 6:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]])
if len(kolom) == 7:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]])
if len(kolom) == 8:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]])
if len(kolom) == 9:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]])
if len(kolom) == 10:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]])
if len(kolom) == 11:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]])
if len(kolom) == 12:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]])
if len(kolom) == 13:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]],output[i][kolom[12]])
if len(kolom) == 14:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]],output[i][kolom[12]],output[i][kolom[13]])
if len(kolom) == 15:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]],output[i][kolom[12]],output[i][kolom[13]],output[i][kolom[14]])
if len(kolom) == 16:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]],output[i][kolom[12]],output[i][kolom[13]],output[i][kolom[14]],output[i][kolom[15]])
if len(kolom) == 17:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]],output[i][kolom[12]],output[i][kolom[13]],output[i][kolom[14]],output[i][kolom[15]],output[i][kolom[16]])
if len(kolom) == 18:
for i in range(index_start, index_eind):
print(i, output[i][kolom[0]],output[i][kolom[1]],output[i][kolom[2]],output[i][kolom[3]],output[i][kolom[4]],output[i][kolom[5]],output[i][kolom[6]],output[i][kolom[7]],output[i][kolom[8]],output[i][kolom[9]],output[i][kolom[10]],output[i][kolom[11]],output[i][kolom[12]],output[i][kolom[13]],output[i][kolom[14]],output[i][kolom[15]],output[i][kolom[16]],output[i][kolom[17]])
``````

I tried a few things with for-loops, but I couldn’t figure it out. Could anyone help me?

``````for i in range(index_start, index_eind):
print(i, *[ouput[i][kolom[j]] for j in range(len(kolom))])
``````

Thank you!!

Slight further improvement:

``````for i in range(index_start, index_eind):
print(i, *[ouput[i][k]] for k in kolom]])
``````
1 Like

Yes, `range(len(...` is a code smell - I was in the process of suggesting that improvement when I noticed you already had. In fact, I am always suspicious of `range`, and might write:

``````for i, out in enumerate(output[index_start:ined_eind], index_start):
print(i, *[out[k] for k in kolom]])
``````

But that does involve creating a whole new list from the slice (and using `enumerate` with a start index)