How to split elements of a list?


Question

I have a list:

my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']

How can I delete the \t and everything after to get this result:

['element1', 'element2', 'element3']
1
72
4/4/2019 3:15:56 PM

Accepted Answer

Something like:

>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
84
7/14/2011 3:47:02 PM

myList = [i.split('\t')[0] for i in myList] 

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon