What is the best way to get all but the last N elements of an iterator in Python? Here is an example of it in theoretical action:
>>> list(all_but_
For a list you could do this:
def all_but_the_last_n(aList, N): return aList[:len(aList) - N] myList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] N = 4 print(all_but_the_last_n(myList, N))
Will print:
[0, 1, 2, 3, 4, 5]