1

Write a function that removes all occurrences of a given letter from a string:

remove_letter("a", "apple") == "pple" 
remove_letter("a", "banana") == "bnn" 
remove_letter("z", "banana") == "banana"

I have tried to make a list from the string and then deleting the element. Below is my code for this problem. Is this correct? Or should I think of something else? Can you tell a better method than this and why is that method better?

def rmv(word, letter):
    word = list(word)
    for ele in word:
        if ele == letter:
            word.remove(ele)
    print(word)

This code does not show any error messages. It gives the expected output for the words like "banana" but for word like "bananna" it would not give the expected output.

For rmv("banana", "n") output is "baaa".

For rmv("bananna", "n") output is "baana".

Rob
  • 14,746
  • 28
  • 47
  • 65
NEVEAH
  • 13
  • 1
  • 4
  • The function is supposed to *return* a new string. Your function prints out a list, and returns nothing. Also you shouldn't remove from a list while you are iterating through it. – khelwood Aug 27 '19 at 11:52
  • the problem is with your for loop. In second case it is skipping the scanning of `a` so you get `baana` instead of `baaa` – shuberman Aug 27 '19 at 11:58

4 Answers4

0

Your problem is that you modify (remove) a list while you iterate over it. Just put a few print lines into your code to see what it does.

It's better to create a new list and adding only characters that you want to keep:

def rmv(word, letter):
    out = []
    for char in word:
        if char != letter:
            out.append(char)
    return ''.join(out)

or, even more compactly:

def rmv(word, letter):
    return ''.join(char for char in word if char != letter)
eumiro
  • 207,213
  • 34
  • 299
  • 261
0

Your code approach with fix:

def rmv(word, letter):
    word = list(word)
    newWord=[]
    for i in range(len(word)):
        if word[i]!= letter:
            newWord.append(word[i]) 
    print(newWord)

rmv("bananna", "n")

What you were doing wrong:

You were iterating your list and deleting as and when your if-else met the condition:

if ele == letter:
            word.remove(ele)

But the problem with this approach is that your pointer moves ahead and the deletion causes the list to shift accordingly at the same time and so when you see the case of

rmv("bananna", "n")

By the time pointer could get the opportunity to scan and remove the last n the list caused it to shift at an index that was already scanned by your list and hence it got ignored.

On a side note - I generally advise people to use Python-tutor to debug their code. It's GUI based code visualizations helps to know where you went wrong.

shuberman
  • 1,416
  • 6
  • 21
  • 38
0

Why not just:

def rmv(word, letter):
    return word.replace(letter, '')

print(rmv("apple", "a"))

There is no need for you to iterate through each character of the string when one function call will do it for you.

Booboo
  • 38,656
  • 3
  • 37
  • 60
0

Why don't you try the string replace function?

Try this code:

def rmv(word, letter):
    return word.replace(letter, "")