Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split elements of a list?

Tags:

python

list

split

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']
like image 939
user808545 Avatar asked Jul 14 '11 15:07

user808545


3 Answers

Something like:

>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
like image 85
Roman Bodnarchuk Avatar answered Sep 25 '22 22:09

Roman Bodnarchuk


myList = [i.split('\t')[0] for i in myList] 
like image 26
dave Avatar answered Sep 23 '22 22:09

dave


Try iterating through each element of the list, then splitting it at the tab character and adding it to a new list.

for i in list:
    newList.append(i.split('\t')[0])
like image 30
caltangelo Avatar answered Sep 24 '22 22:09

caltangelo