Daniel Daniel - 1 year ago 105
Python Question

Remove the last N elements of a list

Is there a a better way to remove the last N elements of a list.

for i in range(0,n):
list.pop( )

Answer Source

if you wish to remove the last n elements, in other words, keep first len - n elements:

lst = lst[:n-1]

Note: This is not an in memory operation. It would create a copy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download