the first n and the last n element of the python list
l=[1,2,3,4,5,6,7,8,9,10]
can be indexed by the expressions
print l[:3]
[1, 2, 3]
and
print l[-3:]
[8, 9, 10]
is there a way to combine both in a single expression, i.e index the first n and the last n elements using one indexing expression?
Just concatenate the results:
l[:3] + l[-3:]
There is no dedicated syntax to combine disjoint slices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With