Hey I just want to simple loop (for x in lists) so that it will loop until n-1 element in list?
I can check it using if or make new list without the last element, but the code become little unsimply, so do you have any suggestion ?
Hey I just want to simple loop (for x in lists) so that it will loop until n-1 element in list?
I can check it using if or make new list without the last element, but the code become little unsimply, so do you have any suggestion ?
You can iterate excluding the last element, like this
for item in my_list[:-1]:
This will actually create a new list, without the last element. So, the efficient way to do this would be to use itertools.islice
like this
from itertools import islice
my_list = [1, 2, 3, 4]
for item in islice(my_list, len(my_list) - 1):
print(item)