python - Remove words from a list which have recurring characters in Python3 -
when run code:
li=["it","is","funny","how","i","fiddled","with","this"] in range(0,len(li)): word=li[i] j in range(0,len(word)-1): if word[j] == word[j+1]: li.pop(i) break print(li)
my desired output is:
['it','is','how','i','with','this']
however, following error message:
traceback (most recent call last): file "fiddle.py", line 3, in <module> word=li[i] indexerror: list index out of range
please point out mistake in code.
how this:
new_list = [x x in li if len(x) == len(set(x))]
also, should never iterate list , remove elements while @ it. best approach create new one
if want remove 2 identical chars together:
new_list = [x x in li if not re.search(r'([a-za-z])\1', x)]
Comments
Post a Comment