Deleting using Enumerate function skipping character
Question: _I Read Several Post on this skipping character while deleting but didn't find
solution_

**I am trying to make anti-vowel program in python 2.7 but stuck at multiple
points which are.!!**

  1. As in my program, I made a copy of list to iterate over it, so that I could delete vowels from original list if found in globally defined vowel list but it is skipping characters!! (May be because of incremented index)
  2. When working with lower function, my program sometimes give list assignment index out of range error!!. Is this something related to memory allocation or something else. (Coz, when I removed it, program run well!!)

Please help to get it correct.

Code:

    
    
    import copy
    vowels=['a','e','i','o','u'] # Making list of vowels
    #Function for anti_vowel
    def anti_vowel(text):
        new_list=list(text) # Converting it into list
        new_list_copy=copy.deepcopy(new_list) # Copying the list to iterate
        res="" # For converting back to string
    
        for index, j in enumerate(new_list_copy):
            m = j.lower() # Converting it into a lower-case for comparison
            if m in vowels:
                del(new_list[index]) # Deleting if a vowel is in list(HERE IS THE PROBLEM)
        return res.join(new_list)
    print anti_vowel("HeEY lOok Word!")
    

P.S: I made it work by replacing DELETE Function by Appending in new list:

    
    
    if j.lower() not in vowels:
                x.append(j) #x is a new list
    

**_BUT NEEDS SOLUTION WITH DELETE FUNCTION_**

Advance Thanks!!


Answer: The reason your current code does not work is the same as the reason that
modifying a list while iterating over it does not work (even though you're not
doing that, exactly): The indexes of the later items in the list are changed
when the earlier items are removed.

A possible way to avoid this issue is to iterate over the list in reverse and
subtract from the original length to index later items. Since the first items
you remove will be towards the end of the list, later items will not have
their indexes change:

    
    
    new_list = list(text)
    L = len(new_list)
    for i, j in enumerate(reversed(new_list)):
        if j.lower() in vowels:
            del new_list[L - i - 1]
    

Note that using negative indexing (rather than manually subtracting from the
original length) will not work, as the length of the list will change as you
remove the items from the end.

I realize this isn't what you need for your current assignment, but the most
"Pythonic" way to solve your problem is to put a generator expression inside a
`str.join` call and not use any lists at all:

    
    
    def anti_vowel(text):
        return "".join(c for c in text if c.lower() not in vowels)
    

