nHow to print out the first character of each list in Python?



Assuming that the list is collection of strings, the first character of each string is obtained as follows −

>>> L1=['aaa','bbb','ccc']
>>> for string in L1:
print (string[0])

a
b
c

If list is a collection of list objects. First element of each list is obtained as follows −

>>> L1=[[1,2,3],[4,5,6],[7,8,9]]
>>> for list in L1:
print (list[0])

1
4
7

Advertisements