+ 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.
8 Answers
+ 5
print(list[2][:5])
This doesn't change the list, just prints out the first 5 characters of the word.
+ 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']
'''
+ 4
Here's the code for your question.
list=['apricot','plum','appleseed']
print(list[2][0:5])
https://code.sololearn.com/cvJDSHB3qc4e/?ref=app
+ 2
When you print you can replace("seed",""))
+ 2
Try
for (String s : list ){
System.out.println(s.replace("seed",""));
}
+ 1
Awesome coding
+ 1
You guys are majestic
0
Cool thanks alot.