Remove element from list if content same character?

remove element from list python
remove character from list python
remove character from string python
python remove item from list if contains
remove all occurrences of a character in a string python
remove multiple elements from list python
remove character from string c
the remove method removes all occurrences of the item from a list

I'm trying to remove all items from a list that contain a particular character, namely "z", "l", or "t". Here's what I have so far:

def run():
    words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you', 'it', 'not', 'or', 'be', 'are', 'from', 'at', 'as', 'your', 'all', 'have', 'new', 'more', 'an', 'was', 'we', 'will', 'home', 'can', 'us', 'about', 'if', 'page', 'my', 'has', 'search', 'free', 'but', 'our', 'one', 'other', 'do', 'no', 'information', 'time', 'they', 'site', 'he', 'up', 'may', 'what', 'which', 'their', 'news', 'out', 'use', 'any', 'there', 'see', 'only', 'so', 'his', 'when', 'contact', 'here', 'business', 'who', 'web', 'also', 'now', 'help', 'get', 'pm', 'view', 'online', 'c', 'e', 'first', 'am', 'been', 'would', 'how', 'were', 'me', 's', 'services', 'some', 'these', 'click', 'its', 'like', 'service', 'x', 'than', 'find', 'price', 'date', 'back', 'top', 'people', 'had', 'list', 'name', 'just', 'over', 'state', 'year', 'day', 'into', 'email', 'two', 'health', 'n', 'world', 're', 'next', 'used', 'go', 'b', 'work', 'last', 'most', 'products', 'music', 'buy', 'data', 'make', 'them', 'should', 'product', 'system', 'post', 'her', 'city', 't', 'add', 'policy', 'number', 'such', 'please', 'available', 'copyright', 'support', 'message', 'after', 'best', 'software', 'then', 'jan', 'good', 'video', 'well', 'd', 'where', 'info', 'rights', 'public', 'books', 'high', 'school', 'through', 'm', 'each', 'links', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'items', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'set', 'under', 'general', 'research', 'university', 'january', 'mail', 'full', 'map', 'reviews', 'program', 'life', 'know', 'games', 'way', 'days', 'management', 'p', 'part', 'could', 'great', 'united', 'hotel', 'real', 'f', 'item', 'international', 'center', 'ebay', 'must', 'store', 'travel', 'comments', 'made', 'development', 'report', 'off', 'member', 'details', 'line', 'terms', 'before', 'hotels', 'did', 'send', 'right', 'type', 'because', 'local', 'those', 'using', 'results', 'office', 'education', 'national', 'car', 'design', 'take', 'posted', 'internet', 'address', 'community']
    f = ['z', 'l', 't']
    finished = False

    while finished == False:
        rep = False
        while rep == False:
            for x in range(len(words)):
                for filterWord in f:
                    try:
                        if filterWord in words[x]:
                            words.pop(x)
                            break
                    except Exception as e:
                        rep = True
            finished = True
    print(words)
    print(len(words))

I expect the output of this function to return all words that do not contain ['z', 'l', 't'] like this:

['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'i', 'you', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'service', 'x', 'find', 'price', 'back', 'had', 'name', 'over', 'year', 'day', 'n', 're', 'used', 'go', 'b', 'work', 'music', 'buy', 'make', 'her', 'add', 'number', 'such', 'message', 'jan', 'good', 'video', 'd', 'where', 'info', 'books', 'high', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'f', 'ebay', 'made', 'off', 'member', 'before', 'did', 'send', 'because', 'using', 'office', 'car', 'design', 'address']

but the actual output is :

['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'i', 'you', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'service', 'x', 'find', 'price', 'back','had', 'name', 'over', 'year', 'day', 'n', 're', 'used', 'go', 'b', 'work','music', 'buy', 'make', 'her','add', 'number', 'such', 'message', 'jan', 'good', 'video', 'd', 'where', 'info', 'books', 'high', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'f', 'ebay', 'comments', 'made', 'off', 'member', 'before', 'did', 'send', 'because', 'using', 'office', 'car', 'design', 'address']['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'with', 'i', 'you', 'not', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'time', 'site', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'get', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'click', 'like', 'service', 'x', 'find', 'price', 'back', 'people', 'had', 'name', 'over', 'year', 'day', 'email', 'health', 'n', 're', 'used', 'go', 'b', 'work', 'most', 'music', 'buy', 'make', 'should', 'system', 'her', 't', 'add', 'number', 'such', 'available', 'support', 'message', 'best', 'then', 'jan', 'good', 'video', 'd', 'where', 'info', 'public', 'books', 'high', 'through', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'full', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'could', 'united', 'real', 'f', 'international', 'ebay', 'store', 'comments', 'made', 'report', 'off', 'member', 'line', 'before', 'did', 'send', 'type', 'because', 'those', 'using', 'office', 'national', 'car', 'design', 'posted', 'address']

Also if the function doesn't find any element that has a forbidden character, it gets stuck in the while loop.

Thanks for the help. :0)


You just need to loop over the words and use all() for the if condition:

Check if multiple strings exist in another string

final_list = list()

words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you', 'it', 'not', 'or', 'be', 'are', 'from', 'at', 'as', 'your', 'all', 'have', 'new', 'more', 'an', 'was', 'we', 'will', 'home', 'can', 'us', 'about', 'if', 'page', 'my', 'has', 'search', 'free', 'but', 'our', 'one', 'other', 'do', 'no', 'information', 'time', 'they', 'site', 'he', 'up', 'may', 'what', 'which', 'their', 'news', 'out', 'use', 'any', 'there', 'see', 'only', 'so', 'his', 'when', 'contact', 'here', 'business', 'who', 'web', 'also', 'now', 'help', 'get', 'pm', 'view', 'online', 'c', 'e', 'first', 'am', 'been', 'would', 'how', 'were', 'me', 's', 'services', 'some', 'these', 'click', 'its', 'like', 'service', 'x', 'than', 'find', 'price', 'date', 'back', 'top', 'people', 'had', 'list', 'name', 'just', 'over', 'state', 'year', 'day', 'into', 'email', 'two', 'health', 'n', 'world', 're', 'next', 'used', 'go', 'b', 'work', 'last', 'most', 'products', 'music', 'buy', 'data', 'make', 'them', 'should', 'product', 'system', 'post', 'her', 'city', 't', 'add', 'policy', 'number', 'such', 'please', 'available', 'copyright', 'support', 'message', 'after', 'best', 'software', 'then', 'jan', 'good', 'video', 'well', 'd', 'where', 'info', 'rights', 'public', 'books', 'high', 'school', 'through', 'm', 'each', 'links', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'items', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'set', 'under', 'general', 'research', 'university', 'january', 'mail', 'full', 'map', 'reviews', 'program', 'life', 'know', 'games', 'way', 'days', 'management', 'p', 'part', 'could', 'great', 'united', 'hotel', 'real', 'f', 'item', 'international', 'center', 'ebay', 'must', 'store', 'travel', 'comments', 'made', 'development', 'report', 'off', 'member', 'details', 'line', 'terms', 'before', 'hotels', 'did', 'send', 'right', 'type', 'because', 'local', 'those', 'using', 'results', 'office', 'education', 'national', 'car', 'design', 'take', 'posted', 'internet', 'address', 'community']

f = ['z', 'l', 't']

def run(words, not_allowed_f):
    for word in words:
        if all(x not in word for x in not_allowed_f):
            final_list.append(word)
    return final_list

print(run(words, f))
Output
['of', 'and', 'a', 'in', 'for', 'is', 'on', 'by', 'i', 'you', 'or', 'be', 'are', 'from', 'as', 'your', 'have', 'new', 'more', 'an', 'was', 'we', 'home', 'can', 'us', 'if', 'page', 'my', 'has', 'search', 'free', 'our', 'one', 'do', 'no', 'he', 'up', 'may', 'which', 'news', 'use', 'any', 'see', 'so', 'his', 'when', 'here', 'business', 'who', 'web', 'now', 'pm', 'view', 'c', 'e', 'am', 'been', 'how', 'were', 'me', 's', 'services', 'some', 'service', 'x', 'find', 'price', 'back', 'had', 'name', 'over', 'year', 'day', 'n', 're', 'used', 'go', 'b', 'work', 'music', 'buy', 'make', 'her', 'add', 'number', 'such', 'message', 'jan', 'good', 'video', 'd', 'where', 'info', 'books', 'high', 'm', 'each', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'under', 'research', 'january', 'map', 'reviews', 'program', 'know', 'games', 'way', 'days', 'p', 'f', 'ebay', 'made', 'off', 'member', 'before', 'did', 'send', 'because', 'using', 'office', 'car', 'design', 'address']

Remove All Occurrences of a Specific Value from a List, In the test above we always call list.remove(1), but the element's index problem only if we call List.remove() with a primitive byte, short, char or int element and list.get(i) are both equal to 1 at line 3, so Java enters the body  Remove an element from List by Index using list.pop () Python. list.pop (index) 1. list.pop(index) In python list’s pop () function will remove the element at given index and also returns the deleted element. If index is not given then it deletes the last element.


Here's a much more pythonic way of achieving your goal.

def run():
    words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you', 'it', 'not', 'or', 'be', 'are', 'from', 'at', 'as', 'your', 'all', 'have', 'new', 'more', 'an', 'was', 'we', 'will', 'home', 'can', 'us', 'about', 'if', 'page', 'my', 'has', 'search', 'free', 'but', 'our', 'one', 'other', 'do', 'no', 'information', 'time', 'they', 'site', 'he', 'up', 'may', 'what', 'which', 'their', 'news', 'out', 'use', 'any', 'there', 'see', 'only', 'so', 'his', 'when', 'contact', 'here', 'business', 'who', 'web', 'also', 'now', 'help', 'get', 'pm', 'view', 'online', 'c', 'e', 'first', 'am', 'been', 'would', 'how', 'were', 'me', 's', 'services', 'some', 'these', 'click', 'its', 'like', 'service', 'x', 'than', 'find', 'price', 'date', 'back', 'top', 'people', 'had', 'list', 'name', 'just', 'over', 'state', 'year', 'day', 'into', 'email', 'two', 'health', 'n', 'world', 're', 'next', 'used', 'go', 'b', 'work', 'last', 'most', 'products', 'music', 'buy', 'data', 'make', 'them', 'should', 'product', 'system', 'post', 'her', 'city', 't', 'add', 'policy', 'number', 'such', 'please', 'available', 'copyright', 'support', 'message', 'after', 'best', 'software', 'then', 'jan', 'good', 'video', 'well', 'd', 'where', 'info', 'rights', 'public', 'books', 'high', 'school', 'through', 'm', 'each', 'links', 'she', 'review', 'years', 'order', 'very', 'privacy', 'book', 'items', 'company', 'r', 'read', 'group', 'sex', 'need', 'many', 'user', 'said', 'de', 'does', 'set', 'under', 'general', 'research', 'university', 'january', 'mail', 'full', 'map', 'reviews', 'program', 'life', 'know', 'games', 'way', 'days', 'management', 'p', 'part', 'could', 'great', 'united', 'hotel', 'real', 'f', 'item', 'international', 'center', 'ebay', 'must', 'store', 'travel', 'comments', 'made', 'development', 'report', 'off', 'member', 'details', 'line', 'terms', 'before', 'hotels', 'did', 'send', 'right', 'type', 'because', 'local', 'those', 'using', 'results', 'office', 'education', 'national', 'car', 'design', 'take', 'posted', 'internet', 'address', 'community']
    filter_letters = ['z', 'l', 't']
    filtered_words = []

    for word in words:
        valid_word = True
        for letter in filter_letters:
            if letter in word:
                valid_word = False
        if valid_word:
            filtered_words.append(word)

    print(filtered_words)

Remove Elements From Lists | Python List remove() Method, Arrays contain elements of the same data types, whereas Python lists can contain items of different types. A single list may contain data types like  How to Delete List Element Using del() The del() deletes the list element using the given index. Each element of the list is located with a certain index assigned to them. To access each element from the list, you have to use the same index term. If you know the index of the list element, you can delete list element easily using the del(). Check the example given below to delete your own list elements.


What you have for the moment is a bit over-complicated, here is a different approach :

for i, word in enumerate(words): # iterates over the list with an index
    if any (f_word in word for f_word in f):
        # the a word contains a 'forbidden' string of the f list
        del words[i] # removes the element from the list

Ways to remove i'th character from string in Python, In this method, each element of string is first converted as each element of list, and then each of them is joined to form a string except the specified index. Code #4 :  Tweet In Python, list 's methods clear (), pop (), and remove () are used to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice. Remove all items: clear ()


Remove all occurrences of a character in a string, Write a program to remove all the occurrences of a character in the string. of character in one string is a factor or multiple of frequency of same character in  1. Select the cells you want to remove the specific characters, and then click Kutools > Text > Remove Characters. See screenshot: 2. Then in the Remove Characters dialog, check Custom option only, then type the characters you want to remove from strings, (it is case sensitive), you can see the result in the Preview pane. See screenshot: 3


5 Ways to Remove a Character from String in Python, Removal of Character from a String using join() method and list comprehension. In this technique, every element of the string is converted to an equivalent element  replace() can possibly be used for performing the task of removal as we can replace the particular index with empty char, and hence solve the issue. Drawback: The major drawback of this approach is that it fails in case there as duplicates in a string that match the char at pos. i. replace() replaces all the occurrences of a particular character and hence would replace all the occurrences of


List<T>.Remove(T) Method, This method also returns false if item was not found in the List<T>. is used to remove the first instance of the duplicate item, and the contents are displayed. The built-in method, remove () in Python, removes the element from the set only if the element is present in the set, just as the discard () method does but If the element is not present in the set, then an error or exception is raised. If the element is present in the set: filter_none. edit. close.