I have a CSV file which I\'m reading in like below. I need to get the first word of all the strings. I know how to get first letter but I\'m not sure how I can get words.
You can use str.split in a list comprehension, noting that you can specify maxsplit
to reduce the number of operations:
L = ['diffuse systemic sclerosis', 'back', 'public on july 15 2008']
res = [i.split(maxsplit=1)[0] for i in L]
# ['diffuse', 'back', 'public']
You can also perform the same operations functionally:
from operator import itemgetter, methodcaller
splitter = methodcaller('split', maxsplit=1)
res = list(map(itemgetter(0), map(splitter, L)))
Across multiple lists, if you wish to maintain the order with which you observe unique first words, you can use the itertool unique_everseen recipe, also found in the more_itertools library:
from itertools import chain
from more_itertool import unique_everseen
L1 = ['diffuse systemic sclerosis', 'back', 'public on july 15 2008']
L2 = ['diffuse systemic sclerosis', 'forearm', 'public on may 9 2014']
res = list(unique_everseen(i.split(maxsplit=1)[0] for i in chain(L1, L2)))
# ['diffuse', 'back', 'public', 'forearm']