List:
x = [1, 6, 2, 7, 1, 6, 1]
len(x)
> 7
How would I split the list for the first 3 and last 3, thus value 7 is left alone using list slicing methods?
Output
x[0:2,4:6] #<-- This doesn't work
> [1, 6, 2, 1, 6, 1] #<-- Expected output
List:
x = [1, 6, 2, 7, 1, 6, 1]
len(x)
> 7
How would I split the list for the first 3 and last 3, thus value 7 is left alone using list slicing methods?
Output
x[0:2,4:6] #<-- This doesn't work
> [1, 6, 2, 1, 6, 1] #<-- Expected output
Meeting OP requeriment: "Is there a way to just keep it same brackets? x[...,...] similar to this one? " (not just using x[:3]+x[-3:]
):
Use numpy.delete
together with numpy.r_
. Specify which first number of elements n1
and which last number of elements n2
you want to keep this way
import numpy as np
x = [1, 6, 2, 7, 1, 6, 1]
n1 = 3 # Keep first n1 elements
n2 = 3 # Keep last n2 elements
print(list(np.delete(x,(np.r_[n1:len(x)-n2])))) # [1 6 2 1 6 1]
You could do: x[0:3]+x[4:7] or x[:3]+x[-3:]. The second one gets the first 3 elements from the last and the first three elements from the right.