How to split elements of a list?

Question:

I have a list:

my_list = ['element1t0238.94', 'element2t2.3904', 'element3t0139847']

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

['element1', 'element2', 'element3']
Asked By: user808545

||

Answers:

myList = [i.split('t')[0] for i in myList] 
Answered By: dave

Something like:

>>> l = ['element1t0238.94', 'element2t2.3904', 'element3t0139847']
>>> [i.split('t', 1)[0] for i in l]
['element1', 'element2', 'element3']
Answered By: Roman Bodnarchuk

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])
Answered By: caltangelo

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

clist = ['element1t0238.94', 'element2t2.3904', 'element3t0139847', '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')]
Answered By: Artsiom Rudzenka

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
Answered By: Max Kleiner

Solution with map and lambda expression:

my_list = list(map(lambda x: x.split('t')[0], my_list))
Answered By: Lukas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.