108

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']
martineau
  • 119,623
  • 25
  • 170
  • 301
user808545
  • 1,551
  • 4
  • 15
  • 15

6 Answers6

153

Something like:

>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
  • 2
    I almost understand this. What is the 1 inside the argument to split doing? Same for what I assume is index 0 after the parens. Is this creating a new list by using the zeroeth element of what had been i? Then is the 1 just another split, effectively getting rid of the rest of the string as the OP wanted? – Malik A. Rumi Nov 19 '16 at 01:59
  • 2
    Since the list contains strings, the variable i is a string. So i.split('\t', 1) calls the split() method of strings. Per the [documentation](https://docs.python.org/2/library/stdtypes.html#str.split), the first parameter of this method is the string to split by and the second is the maximum number of splits to perform. The method returns the list of strings that result from performing the split, so "[0]" returns the first split string in the result list. – jcl Mar 29 '17 at 22:30
  • 2
    When I tried this for my own code, it returned an error that the 'list' object has no attribute 'split'. I'm confused as to how to get around this. – keitereth24 Jul 28 '17 at 19:19
  • 1
    @keitereth24 , Roman splitted list of string. You may have a list of list – AAI Apr 26 '18 at 16:20
  • Is there a way to do this for tuple? – Sophile Feb 25 '23 at 18:23
44
myList = [i.split('\t')[0] for i in myList] 
dave
  • 12,406
  • 10
  • 42
  • 59
11

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])
caltangelo
  • 264
  • 1
  • 11
7

Do not use list as variable name. You can take a look at the following code too:

clist = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', 'element5']
clist = [x[:x.index('\t')] if '\t' in x else x for x in clist]

Or in-place editing:

for i,x in enumerate(clist):
    if '\t' in x:
        clist[i] = x[:x.index('\t')]
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
5

Solution with map and lambda expression:

my_list = list(map(lambda x: x.split('\t')[0], my_list))
Lukas
  • 2,034
  • 19
  • 27
3

I had to split a list for feature extraction in two parts lt,lc:

ltexts = ((df4.ix[0:,[3,7]]).values).tolist()
random.shuffle(ltexts)

featsets = [(act_features((lt)),lc) 
              for lc, lt in ltexts]

def act_features(atext):
  features = {}
  for word in nltk.word_tokenize(atext):
     features['cont({})'.format(word.lower())]=True
  return features
Max Kleiner
  • 1,442
  • 1
  • 13
  • 14