Firstly, you do not need to do this:
vaw = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z']
... to get a sequence of characters. A string is already a sequence of characters. (The same is true for var1 , but there your code requires a variable sequence of characters, where you can replace any character with a long string, so you need a list .)
Also, your code actually doesn't work, because string == string in vaw matches True in vaw , which is always false. I think you meant if string in vaw . While we are on it, I would not name the variable string , because this is the name of the built-in module.
And you can save a few keystrokes without adding extra spaces in places where the standard Python style (PEP8) is not usually used. :)
So:
def translate(var1): vaw = 'bcdfghjklmnpqrstvwxz' var1 = list(var1) for s in var1: if s in vaw: var1[var1.index(s)] = s + 'o' + s print ''.join(var1)
Further, if you need the index of each element in var1 , you do not want to throw it away, and then find it again with index . Besides being larger code and slower, it will also give you the wrong answer for any item that appears more than once. So:
def translate(var1): vaw = 'bcdfghjklmnpqrstvwxz' var1 = list(var1) for i, s in enumerate(var1): if s in vaw: var1[i] = s + 'o' + s print ''.join(var1)
This is about how much you can go if you want to change the var list in place. You can change it to make var1[i+1:i+1] = 'o' + s to insert new elements after the existing one, but then you have to iterate over the copy of var1 (you cannot change the shape of anything during iteration on it) and you should keep track of index changes, etc.
It is usually easier to just build a new structure than to change the old one in its place. To do this, there are lists, expression expressions, map , filter , etc. For each element s source list, you want s + 'o' + s if it's in vaw , just s otherwise, right? You can translate this directly into Python:
def translate (var1): vaw = 'bcdfghjklmnpqrstvwxz' new_var1 = (s + 'o' + s if s in vaw else s for s in var1) return ''.join(new_var1)