Get the nth element from the inner list of a list of lists in Python [duplicate]

Simply change your list comp to be:

b = [el[0] for el in a]

Or:

from operator import itemgetter
b = map(itemgetter(0), a)

Or, if you’re dealing with “proper arrays”:

import numpy as np
a = [ [1,2], [2,9], [3,7] ]
na = np.array(a)
print na[:,0]
# array([1, 2, 3])

And zip:

print zip(*a)[0]

Leave a Comment