How to extract first item of each sublist in Python

3 Answers

0 votes
lst = [['a', 'c', 'd'], ['u', 'v', 'w', 'x'], [1, 2, 3, 4]]
 
first_items = [item[0] for item in lst]

print(first_items)

 
 
'''
run:
 
['a', 'u', 1]
 
'''

 



answered Feb 16, 2019 by avibootz
0 votes
lst = [['a', 'c', 'd'], ['u', 'v', 'w', 'x'], [1, 2, 3, 4]]
 
first_items = list(zip(*lst))[0]

print(first_items)

 
 
'''
run:
 
['a', 'u', 1]
 
'''

 



answered Feb 16, 2019 by avibootz
0 votes
import numpy as np

lst = np.array([['a', 'c', 'd'], ['u', 'v', 'w', 'x'], [1, 2, 3, 4]])

print(lst[0][0])
print(lst[1][0])
print(lst[2][0])

 
 
'''
run:
 
a
u
1
 
'''

 



answered Feb 16, 2019 by avibootz
...