Python: shorter syntax for slices with gaps?
Question: Suppose I want the first element, the 3rd through 200th elements, and the
201st element through the last element by step-size 3, from a list in Python.

One way to do it is with distinct indexing and concatenation:

    
    
    new_list = old_list[0:1] + old_list[3:201] + old_list[201::3]
    

Is there a way to do this with just one index on `old_list`? I would like
something like the following (I know this doesn't syntactically work since
list indices cannot be lists and since Python unfortunately [doesn't have
slice literals](http://stackoverflow.com/questions/13706258/passing-python-
slice-syntax-around-to-functions); I'm just looking for something close):

    
    
    new_list = old_list[[0, 3:201, 201::3]]
    

I can achieve some of this by switching to NumPy arrays, but I'm more
interested in how to do it for native Python lists. I could also create [a
slice maker](http://stackoverflow.com/questions/13706258/passing-python-slice-
syntax-around-to-functions) or something like that, and possibly strong arm
that into giving me an equivalent slice object to represent the composition of
all my desired slices.

But I'm looking for something that doesn't involve creating a new class to
manage the slices. I want to just sort of concatenate the slice syntax and
feed that to my list and have the list understand that it means to separately
get the slices and concatenate their respective results in the end.


Answer: A slice maker object (e.g. `SliceMaker` from your other question, or `np.s_`)
can accept multiple comma-separated slices; they are received as a `tuple` of
`slice`s or other objects:

    
    
    from numpy import s_
    s_[0, 3:5, 6::3]
    Out[1]: (0, slice(3, 5, None), slice(6, None, 3))
    

NumPy uses this for multidimensional arrays, but you can use it for slice
concatenation:

    
    
    def xslice(arr, slices):
        return sum((arr[s] if isinstance(s, slice) else [arr[s]] for s in slices), [])
    xslice(range(10), s_[0, 3:5, 6::3])
    Out[1]: [0, 3, 4, 6, 9]
    

