Loop over list items, except for the last two items (python)
Question:
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?
Answers:
Python lists support slicing like this:
for i in your_list[:-2]
random_list = [1, 2, 3, 4, 5, 6]
for element in random_list[:-2]:
print(element)
[Out] :
1 2 3 4
optionally
items = [] # your 14 item long list here
for i in range(len(items)-2):
print(items[i])
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?
Python lists support slicing like this:
for i in your_list[:-2]
random_list = [1, 2, 3, 4, 5, 6]
for element in random_list[:-2]:
print(element)
[Out] :
1 2 3 4
optionally
items = [] # your 14 item long list here
for i in range(len(items)-2):
print(items[i])