How about you check if the word is already in the list before appending it, like so:
def endwords(sent): wordList = [] words = sent.split() for word in words: if "." in word and word not in wordList: wordList.append(word) return wordList
You're trying to check if word == list, but that's seeing if the word is equal to the entire list. To check if an element is in a container in python, you can use the in keyword. Alternatively, to check if something is not in a container, you can use not in.
Another option is to use a set:
def endwords(sent): wordSet = set() words = sent.split() for word in words: if "." in word: wordSet.add(word) return wordSet
And to make things a little cleaner, here is a version using set comprehension:
def endwords(sent): return {word for word in sent.split() if '.' in word}
If you want to get a list out of this function, you can do so like this:
def endwords(sent): return list({word for word in sent.split() if '.' in word})
Since you said in your question you want to check if the word ends with a '.', you probably also want to use the endswith() function like so:
def endwords(sent): return list({word for word in sent.split() if word.endswith('.')})
list,dict,str, etc)