Indexing a list inside a list [:][0][0] notation?

Viewed 49
list1 = [[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],[[13,14,15],[16,17,18],[19,20,21],[22,23,24]]]

for i in range(0,2):
     print(list1[:][i][1])

output =

[4, 5, 6]
[16, 17, 18]

how do I get the above code to work such that:

desired output =

2 14
5 17

What is the correct indexing notation for the list? I'm having particular trouble with the list[:] format as it seems to be ignored by the above code. Thx

1 Answers

Something like?

>>> print(*map(' '.join, zip(*[map(str, list(zip(*x[:2]))[1]) for x in list1])), sep='\n')
2 14
5 17
>>> 

Or:

>>> print(*map(' '.join, zip(*[(str(x[0][1]), str(x[1][1])) for x in list1])), sep='\n')
2 14
5 17
>>> 

To make it work on your code you would have to do:

for i in range(0,2):
     print(list1[0][i][1], list1[1][i][1])

Out:

2 14
5 17

The reason it your code didn't work is because it is getting the sublist from i, not the sublist's sublist.

Related