-1

I want to for loop over a list of 14 items except for the last two items. Does anybody know if there is a function or command in python which accomplishes this?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252

3 Answers3

1

Python lists support slicing like this:

for i in your_list[:-2]
Eumel
  • 1,298
  • 1
  • 9
  • 19
1
random_list = [1, 2, 3, 4, 5, 6]

for element in random_list[:-2]:
     print(element)

[Out] :

1 2 3 4
Odhian
  • 351
  • 5
  • 14
1

optionally

items = [] # your 14 item long list here
for i in range(len(items)-2):
    print(items[i])
Henok Teklu
  • 528
  • 3
  • 9