114

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

6 Answers 6

161

Something like:

>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
Sign up to request clarification or add additional context in comments.

5 Comments

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?
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, 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.
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 , Roman splitted list of string. You may have a list of list
Is there a way to do this for tuple?
45
myList = [i.split('\t')[0] for i in myList] 

Comments

12

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

1 Comment

This works, but list comprehensions are a much better way to do this
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')]

Comments

5

Solution with map and lambda expression:

my_list = list(map(lambda x: x.split('\t')[0], my_list))

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.