Repeating statements do not properly delete items in the list.

Asked 2 years ago, Updated 2 years ago, 72 views

The code below is a function anti_vowel that removes all vowel alphabets (aeiouAEIOU) from the string. I expected it to work properly, but if you give the sample value of "Hey look Words!" as an input value, it will return "Hyl Words!" In other words, I can't delete the last 'o', so why is this happening?

text = "Hey look Words!"

def anti_vowel(text):

    textlist = list(text)

    for char in textlist:
        if char.lower() in 'aeiou':
            textlist.remove(char)

    return "".join(textlist)

print anti_vowel(text)

python string list

2022-09-22 20:58

1 Answers

This is a problem that occurs when the list is modified as the repetitive statement progresses. Try to make a copy of the list as shown in the code below so that you do not remove the value from the list you are touring.

for char in textlist[:]: #shallow copy of the list
    # # etc

If you want to get a clearer picture of the current problem, insert print char, textlist at the beginning of the repeat statement. You probably thought the original string would come out vertically on the left side of the list, but the output is actually as follows:

H ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
e ['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
  ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # !
l ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
k ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] # Problem!!
  ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
W ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
o ['H', 'y', ' ', 'l', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!'] 
d ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
s ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
! ! ['H', 'y', ' ', 'l', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
Hy lk Words!

Why is he doing this? Python's for x in y repetition actually looks more intuitive, but actually like a regular repetition, it's going to index each element of the list, i.e. to remove the elements of the list while touring the listIf you look at it, there are values that skip and pass. This eventually results in missing the second o of "look" and passing by. This is because the index of the second o is pulled one space while deleting the previous character, and it becomes the index that has already been circulated. After that, if you find o in "Words", you delete the first o in the string, that is, the missing o.

You can write the code you want to create in a better (cleaner) way as shown below.

def remove_vowels(text): # function names should start with verbs! :)
    return ''.join(ch for ch in text if ch.lower() not in 'aeiou')


2022-09-22 20:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.