Home > OS >  remove all characters after first occurrence of a specific character
remove all characters after first occurrence of a specific character

Time:09-28

string_list = [(I, love, python), (We, love, stackoverflow)] 

for every element in this list, id like to remove the first occurrence of , and everything after it

i tried using partition()

ticker_list = []
for x in range(len(raw_ticker_list)):
    text = raw_ticker_list[x]                                             

    newText = text.partition(',')
    ticker_list.append(newText)

but this only partitions everything after , into a new element

CodePudding user response:

Use:

new_list = []
for i in string_list:
    new_list.append(i[0])

CodePudding user response:

newText = text.split(",", 1)
newText = newText[0]
ticker_list.append(newText)
  • Related