Slicing 2D list (inner dimension) in Python and corresponding method in Numpy array and PyTorch tensor


There are a lot of time I slice some portion of data from multi-dimension vector/tensor. The Numpy array and PyTorch tensor make it very easy to slice, and with a very similar syntax. In some scenario I might need to work from a list, and here comes one implementation that can be done.

Example 1

A simple 2D number list, I want to slice the input into 3 list like elements

Numpy array

The syntax is using [: , :x] and [: , y], this means for the first dimension, slice all, and the 2nd dimension, slice :x list or slice yth item

PyTorch tensor

Same slicing syntax as Numpy


Using a zip, * and list comprehension, this does not look nature.

The innermost list comprehension, it return the result:

Explanation of the * => zip

Example 2

This example, I am having some string information:

Numpy array

Numpy array would do the expected result, even with string

PyTorch tensor

Pytorch tensor is for numerical calculation, so the string input would break it.


List operation work as expected.

Final words

The Numpy array do perform very versatile, the list approach is some backup plan for myself, and I didn’t check performance and memory pressure comparing 1) direct list operation vs 2) list => numpy array => list operation.