Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

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
...