How to create a list of all words in a text file except for words with duplicate letters

Viewed 69

I'm trying to iterate through a list, and append all the words to a new list, excluding words that have duplicate letters.

Example:

words = ['cat', 'car', 'weevil', 'lizard', 'mammoth', 'cabbage', 'aardvaark']
newlist = []

for word in words:
    for letter in word
        if word.count(letter) > 1:
            pass
        else:
            newlist.append(word)

print(newlist)
# Result
['cat', 'cat', 'cat', 'car', 'car', 'car', 'weevil', 'weevil', 'weevil', 'weevil', 'lizard', 'lizard', 'lizard', 'lizard', 'lizard', 'lizard', 'mammoth', 'mammoth', 'mammoth', 'mammoth', 'cabbage', 'cabbage', 'cabbage', 'aardvaark', 'aardvaark', 'aardvaark']

The code doesn't actually work because a word like weevil for example will satisfy the word.count(letter) condition for the letter 'w', pass twice for the two 'e's and then satisfy three more times after the two 'e's which is why it's appearing in the list four times.

Perhaps I'm approaching this from the wrong angle, but I'm really stumped at how to do this.

5 Answers
Related