Holy
Holy

Reputation: 37

Removing an item from a list

I would like to remove to ignore duplicates in my list. For example, let's say the function checks for words that end with a ''.'' and puts them in a list. I would like to make sure that duplicate words don't go in the list.

Here is what I have so far:

def endwords(sent):
    list = []
    words = sent.split()
    for word in words:
        if "." in word:
            list.append(word)
        # bottom if statment does not work for some reason. thats the one i am trying to fix    
        if (word == list):
            list.remove(word)
    return list

Upvotes: 0

Views: 85

Answers (5)

Azat Ibrakov
Azat Ibrakov

Reputation: 10963

After statement

list = []

you can't use built-in list class and to understand that you can spend about an hour or so, that's why we avoid names of built-ins for our objects.

More at this answer.


function checks for words that end with a ''.''

Statement

"." in word

checks if word contains dot symbol (e.g. "." in "sample.text" will work ok while it simply doesn't end with dot), if you need to check that it ends with dot – use str.endswith method.


I would like to make sure that duplicate words don't go in the list.

just make sure before storing one that it hasn't been stored already.


Finally we can write

def endwords(sent, end='.'):
    unique_words = []
    words = sent.split()
    for word in words:
        if word.endswith(end) and word not in unique_words:
            unique_words.append(word)
    return unique_words

Test

>>>sent = ' '.join(['some.', 'oth.er'] * 10)
>>>unique_words = endwords(sent)
>>>unique_words
['some.']

P. S.

If order doesn't matter – use set, it will take care of duplicates (works only with hashable types, str is hashable):

def endwords(sent, end='.'):
    unique_words = set()
    words = sent.split()
    for word in words:
        if word.endswith(end) and word not in unique_words:
            unique_words.add(word)
    return unique_words

or with set comprehension

def endwords(sent, end='.'):
    words = sent.split()
    return {word for word in words if word.endswith(end)}

Upvotes: 2

Will Da Silva
Will Da Silva

Reputation: 7040

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('.')})

Upvotes: 2

The less verbose way to do it would be using list comprehension, that is

my_list = [word for word in words if '.' in word]

And to ensure the elements aren't duplicated, just use set.

my_list = set(my_list)  # No more duplicated values

Upvotes: 0

Décio Soares
Décio Soares

Reputation: 125

Why not use a set?

def endwords(sent):
    my_list = set()
    words = sent.split()
    for word in words:
        if "." in word:
            my_list.add(word)
    return my_list

Upvotes: 0

youDaily
youDaily

Reputation: 1384

You can add a sample judge for the question.

def endwords(sent):
    list = []
    words = sent.split()
    for word in words:
        if "." in word:
            if word not in list:
                list.append(word)
        # bottom if statment does not work for some reason. thats the one i am trying to fix   

    return list

Upvotes: 0

Related Questions