2

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
pnuts.93
  • 25
  • 1
  • 6

3 Answers3

2

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]
James
  • 32,991
  • 4
  • 47
  • 70
  • Thank you very much, this works great, I was honestly not aware about the possibility of using a for loop within variable a definition. I would just be curious to understand why in this syntax the order of indexing does not seem to be relevant – pnuts.93 Apr 05 '22 at 07:44
  • When you chain slices like you tried, you end up slicing the outer most layer: entire rows. To access the elements within each row, you need to either iterate over each row or use an object/class that supports nested slicing. – James Apr 05 '22 at 07:47
1

I want explain, what have you done by this

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

Firstly note that python use indices starting at 0, i.e. for [1,2,3] there is 0th element, 1th element and 2nd element.

[0:] means get list elements starting at 0th element, this will give you copy of list, [1:] means get list elements starting at 1th element, which will give you list with all but 0th element. Therefore both lines are equivalent to each other and to

print(list[1:])

You might desired output using comprehension or map as follows

list1 = [[1, 2, 3, 4], ['a', 'b', 'c', 'd'], [9, 8, 7, 6]]
list2 = list(map(lambda x:x[1:],list1))
print(list2)

output

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

lambda here is nameless function, note that comprehension here is more readable, but might be easier to digest if you earlier worked with language which have similar feature, e.g. JavaScript's map

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

First - don't name your list "list"!

a = [[1, 2, 3, 4],
    ['a', 'b', 'c', 'd'],
    [9, 8, 7, 6]]
b = [x[1:] for x in a]
print(b)

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

Drako
  • 773
  • 10
  • 22
  • What's new compared with the previous [answer](https://stackoverflow.com/a/71747864/5929910) – mhhabib Apr 05 '22 at 07:39
  • @mhhabib It was probably written simultaneously - when I started to write it there were 0 answers; but small difference is clearly visible in a first sentence - I'm pointing out to not use list as his name for variable; I'm not working for SO - I do things on my spare time - I wrote the answer while in a work meeting - may be hit enter late :). If you have time - feel free to combine the answers by adding note about wrong naming of variable. – Drako Apr 05 '22 at 07:53