Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slicing 2D Python List

Tags:

python

list

slice

Let's say I have a list:

list = [[1, 2, 3, 4],
        ['a', 'b', 'c', 'd'], 
        [9, 8, 7, 6]]

and I would like to get something like:

newList =  [[2, 3, 4],
            ['b', 'c', 'd'],
            [8, 7, 6]]

hence I tried going with this solution

print(list[0:][1:])

But I get this output

[['a', 'b', 'c', 'd'],
 [9, 8, 7, 6]]

Therefore I tried

print(list[1:][0:])

but I get precisely the same result.

I tried to make some research and experiments about this specific subject but without any result.

like image 313
pnuts.93 Avatar asked Sep 16 '25 16:09

pnuts.93


1 Answers

You want the 1 to end element of every row in your matrix.

mylist = [[1, 2, 3, 4],
        ['a', 'b', 'c', 'd'], 
        [9, 8, 7, 6]]

new_list = [row[1:] for row in mylist]
like image 186
James Avatar answered Sep 18 '25 08:09

James