Python Forum

Full Version: Reverse printed words
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
If words include letter "E" then I need to return these words and another words I need to print out. And I need to reverse returned words.

lib = ["politsei", "mehine","panda","ment","kusi", "treener","leidma", "jooks", "muksid","president"]

def sisend(lib, b):

result =[]

for i in lib:
if b in i:
result.append(i)
return result

print(sisend(lib, 'e'))
There is a python command for iterating through a list in reverse order.
I know that ....reverse() command. Right now code print letters with specific word, but code needs to print words without this letter and reverse returned(with specific letter) words.
Reverse the order of the words or reverse the letters in the word? Either way it is something I would use for this problem. Maybe twice. Could you provide sample output to help me better understand what your program is supposed to do?
Reversing the words is as simple as...

"word"[::-1]
Performing that on each item in a list is made easy with the map() built-in function too.
lib = ["politsei", "mehine","panda","ment","kusi", "treener","leidma", "jooks", "muksid","president"]

def sisend(lib, b):
 result=[]

 for i in lib:
  if b in i:
   result.append(i[::-1])
  else:
    result.append(i)
 return result

print(sisend(lib, 'e'))
(Apr-20-2020, 09:29 PM)deanhystad Wrote: [ -> ]Reverse the order of the words or reverse the letters in the word? Either way it is something I would use for this problem. Maybe twice. Could you provide sample output to help me better understand what your program is supposed to do?

output:"panda","kusi" "jooks", "muksid"(all words without E)and then returned words(with letter "E") in reversed output output: president, leidma, treener, ment, mehine, politsei
I would process the list in reverse order and build a new list. If the word contains the letter, append to the new list. If the word does not contain the letter insert in the front of the list.