Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Slicing 2D Python List

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading