I want to find which word’s last character is ‘e’ and I would like to replace ‘e’ with ‘ing’. After this process want to append these in the array such as new words
JavaScript
x
10
10
1
words= ['example', 'serve', 'recognize', 'ale']
2
3
4
for x in words:
5
size = len(x)
6
if "e" == x[size - 1]:
7
words.append(x.replace(x[-1], 'ing'))
8
9
print(words)
10
output
JavaScript
1
2
1
['example', 'serve', 'recognize', 'ale', 'ingxampling', 'singrving', 'ringcognizing', 'aling']
2
I want to get the output like this
JavaScript
1
2
1
['example', 'serve', 'recognize', 'ale', 'exampling', 'serving', 'recognizing', 'aling']
2
Advertisement
Answer
Try this:
JavaScript
1
8
1
words = ['example', 'serve', 'recognize', 'ale']
2
3
for x in words:
4
if x[-1] == 'e':
5
words.append(x[:-1] + 'ing')
6
7
print(words)
8
Or if you want a 1 liner:
JavaScript
1
2
1
words = [*words, *[x[:-1] + 'ing' for x in words if x[-1] == 'e']]
2