Practice Makes Perfect, anti_vowel, 8/15


#1

I’ve seen other answers, I’m not looking for a better way to solve it. I’m not just trying to get past the lesson; I’m trying to figure out why the way I’m doing it isn’t giving me the results I’d expect.

def anti_vowel(text):
  delimiter = ''
  lVowel = ['a','e','i','o','u','A','E','I','O','U']
  lText = []
  lText += text
  lOutput = lText
  for x in lText:
    for y in lVowel:
      if x==y:
          lOutput.remove(x)
  return delimiter.join(lOutput)
print anti_vowel('Hey look words!')

I’ve always been more comfortable with JavaScript arrays and loops, if that helps. The above doesn’t take out the ‘o’ in ‘words,’ unless I literally repeat my loops. So, I think I’m missing something fundamental in how loops and indices work, even though I thought I knew when I passed those lessons. In my mind, I’m creating two arrays (lists), I’m looping through the first (the input), and for each value, looping through the second to check it for matches. lOutput is a product of trying to debug, not wanting to operate on the array that I’m still using for my loop, but it didn’t help. If it is determined that the current value in first the array is equal to the current value in the second, it triggers a removal from the array.

What am I misunderstanding about lists and loops?


Purify
#2

lets say we have the following word of which we want to remove the vowels:

l   0   0   k
0   1   2   3

i put the indexes under the list/string. Its fundamental to understand that lists or strings can’t have empty spaces/slots.

if we remove the letter o from index 1, everything to the right of the removed item moves one index to the left:

l   o   k
0   1   2

then the loop progresses to index 2.

This causes a successive vowel to be skipped. Removing from the list/string you are looping over is a bad idea.

and this:

lOutput = lText

doesn’t make a copy of the list, now you have two variables referencing the same list.


Remove_duplicates : unable to debug code [SOLVED]
#3

I see. Thank you for the reply. So the issue I thought I had is actually the issue, but the way I solved it doesn’t work. My understanding of lists-n-loops remains intact :sweat_smile:

doesn’t make a copy of the list, now you have two variables referencing the same list.

I see what you’re saying. Is is there a straightforward way to have made a copy? My first instinct is to create an empty list and populate it by looping through the original, but there must be a less convoluted way.


#4

but currently you have the string (text) and a list version of it (lText), which should be enough. You can loop over the string while removing from the list or vice versa.


#5

Again, thank you for your help. I know that’s enough to pass the lesson. But the goal is to learn.
Since I’m copying lists incorrectly, is the only way to copy a list to create another list object and populate it?


#6

there is the built-in list() function. But personally, if its about learning, the learning experience here is not to use this approach.

You end up with 3 things: original string, list copy of original string and final result. While if you append consonants to a new list, you end up with 2 things (original string and final result)

also, strings are immutable. .remove() will create a new string.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.