Remove a word | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Remove a word

list = ['apricot', 'plum', 'appleseed'] print(list[2]) Is there anyway I can remove seed for apple when I print it, so I can print apple only.

20th Oct 2019, 8:33 AM
Steve Nova
Steve Nova - avatar
8 Answers
+ 5
print(list[2][:5]) This doesn't change the list, just prints out the first 5 characters of the word.
21st Oct 2019, 3:40 AM
David Ashton
David Ashton - avatar
+ 4
#some of the mentioned solutions does only work if 'appleseed' is at index [2]. here are some variations that will work in general: list_ = ['apricot', 'plum', 'banana','appleseed', 'orange'] print('(1a)',list_) print('(1b)',list_[2]) #<- this does work no longer because an other item has been inserted # it does only print the corrected list and only item [2] print('(2)',[x.replace('seed','') for x in list_ if x.startswith('apple')]) # or create a new list with corrected item, original list keeps as it is: res = [x.replace('seed','') for x in list_] print('(3)',res) # or use original list to print with corrected item list_ = [x.replace('seed','') for x in list_] print('(4)',list_) # output: ''' (1a) ['apricot', 'plum', 'banana', 'appleseed', 'orange'] (1b) banana (2) ['apple'] (3) ['apricot', 'plum', 'banana', 'apple', 'orange'] (4) ['apricot', 'plum', 'banana', 'apple', 'orange'] '''
20th Oct 2019, 9:49 AM
Lothar
Lothar - avatar
+ 4
Here's the code for your question. list=['apricot','plum','appleseed'] print(list[2][0:5]) https://code.sololearn.com/cvJDSHB3qc4e/?ref=app
22nd Oct 2019, 7:33 AM
Indira
Indira - avatar
+ 2
When you print you can replace("seed",""))
20th Oct 2019, 8:58 AM
Daniel (kabura)
Daniel (kabura) - avatar
+ 2
Try for (String s : list ){ System.out.println(s.replace("seed","")); }
20th Oct 2019, 9:52 AM
Daniel (kabura)
Daniel (kabura) - avatar
+ 1
Awesome coding
20th Oct 2019, 9:05 AM
Steve Nova
Steve Nova - avatar
+ 1
You guys are majestic
20th Oct 2019, 9:59 AM
Steve Nova
Steve Nova - avatar
0
Cool thanks alot.
20th Oct 2019, 8:51 AM
Steve Nova
Steve Nova - avatar