Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (20.3k points)

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']

2 Answers

0 votes
by (40.7k points)

You can try doing something like this:

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

>>> [i.split('\t', 1)[0] for i in l]

['element1', 'element2', 'element3']

0 votes
by (106k points)

To split a element from a list you can use the below-mentioned code:-

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

Browse Categories