My_List = ["adopt", "bake", "beam"]

Problem : For each word in My_List, add ‘d’ to the end of the word if the word ends in “e” to make it past tense. Otherwise, add ‘ed’ to make it past tense. Save these past tense words to a list called Past_Tense.

5 Answers

1
hiro protagonist On Best Solutions

this is a way to do that:

My_List = ["adopt", "bake", "beam"]
res = [word + 'd' if word[-1] == 'e' else word + 'ed' for word in My_List]
# ['adopted', 'baked', 'beamed']
1
Rafael On

So here are some ways to get started:

for word in My_List:
    # do something with your word

word[-1] gives you the last character of the word.

You can join strings together like this: "{} something else here".format(word)

Put all of them together in a logical order and you can solve the problem on your own! Which is more fun!

0
michael petronav On

Try this :

Past_Tense = [k+'d' if k.endswith('e') else k+'ed' for k in My_List]

OUTPUT :

['adopted', 'baked', 'beamed']
1
yatu On

You can use a list comprehension and f-strings to add d to those strings that end with e, which can be checked using the endswith str method:

[f'{i}d' if i.endswith('e') else f'{i}ed' for i in My_List]
# ['adopted', 'baked', 'beamed']

For python versions 3.6 < use:

['{}d'.format(i) if i.endswith('e') else '{}ed'.format(i) for i in My_List]
0
Alain T. On
Past_Tense = [ w + "e"*(w[-1]!="e") + "d" for w in My_List ]