If you want to modify the contents of a list of immutable objects (strings are immutable in python), you need to do something different than for ele in list. because changing ele will not actually change the contents of the list. (Although, you are not even doing that, you're just printing "X", but I assume that's what you want to do). You can do this instead, which will modify the list in-place:
def cleaner(list1):
for i in range(len(list1)):
if list1[i] in ['a', 'the', 'and']:
list1[i] = 'X'
pets = ["the","dog","and","a","cat"]
cleaner(pets)
Or, an even more elegant approach:
def cleaner(item):
if item in ['a', 'the', 'and']:
return 'X'
else:
return item
pets = ["the","dog","and","a","cat"]
cleaned_pets = list(map(cleaner, pets))