Indexing a nested list in python

list, multidimensional-array, python, python-2.7

Solution

With this sort of thing, I generally recommend `numpy`:

>>> data = np.array([ [0, 1], [2,3] ])
>>> data[:,0]
array([0, 2])

As far as how python is handling it in your case:

data[:][0]

Makes a copy of the entire list and then takes the first element (which is the first sublist).

data[0][:]

takes the first sublist and then copies it.

Problem

Given `data` as ``` data = [ [0, 1], [2,3] ] ``` I want to index all first elements in the lists inside the list of lists. i.e. I need to index `0` and `2`. I have tried ``` print data[:][0] ``` but it output the complete first list .i.e. ``` [0,1] ``` Even ``` print data[0][:] ``` produces the same result. My question is specifically how to accomplish what I have mentioned. And more generally, how is python handling double/nested lists?

Original source