From a performance point of view:
mylist = mylist[2:-2]
and del mylist[:2];del mylist[-2:]
are equivalent
- they are around 3 times faster than the first solution
for _ in range(2): mylist.pop(0); mylist.pop()
Code
iterations = 1000000
print timeit.timeit('''mylist=range(9)
for _ in range(2): mylist.pop(0); mylist.pop()''', number=iterations)/iterations
print timeit.timeit('''mylist=range(9)
mylist = mylist[2:-2]''', number=iterations)/iterations
print timeit.timeit('''mylist=range(9)
del mylist[:2];del mylist[-2:]''', number=iterations)/iterations
output
1.07710313797e-06
3.44465017319e-07
3.49956989288e-07
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…