-1

community.

I need to write a function that goes through a string and checks if each word exists in a list, if the word exists in the (Remove list) it should remove that word if not leave it alone.

i wrote this:

def remove_make(x):
    a = x.split()
    for word in a: 
        if word in remove: # True
            a = a.remove(word)  
        else:
            pass
        return a

But it returns back the string with the (Remove) word still in there. Any idea how I can achieve this?

Youssef Razak
  • 365
  • 4
  • 11
  • You need to keep a copy of the list and iterate over it, [see top answer](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Ido Savion Jan 17 '21 at 09:16
  • Can you share a simple data from your file besides you requested answer structure and your tried code?? – Ahmed Jan 17 '21 at 09:45
  • For me your question isn't clear. You need to add more information due to the lack of explanation. **Simple data** of your string and the **required** answer which you're looking for. – Ahmed Jan 17 '21 at 09:53

4 Answers4

2

A more terse way of doing this would be to form a regex alternation based on the list of words to remove, and then do a single regex substitution:

inp = "one two three four"
remove = ['two', 'four']
regex = r'\s*(?:' + r'|'.join(remove) + ')\s*'
out = re.sub(regex, ' ', inp).strip()
print(out)   # prints 'one three'
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can try something more simple:

import re

remove_list = ['abc', 'cde', 'edf']
string = 'abc is walking with cde, wishing good luck to edf.'

''.join([x for x in re.split(r'(\W+)', string) if x not in remove_list])

And the result would be:

' is walking with , wishing good luck to .'

The important part is the last line:

''.join([x for x in re.split(r'(\W+)', string) if x not in remove_list])

What it does:

  • You are converthing the string to list of words with re.split(r'(\W+)', string), preserving all the whitespaces and punctuation as list items.
  • You are creating another list with list comprehension, filtering all the items, which are not in remove_list
  • You are converting the result list back to string with str.join()

The BNF notation for list comprehensions and a little bit more information on them may be found here

PS: Of course, you may make this a little bit more readable if you break the one-liner into peaces and assign the result of re.split(r'(\W+)', string) to a variable and decouple the join and the comprehension.

wankata
  • 855
  • 4
  • 12
0

list.remove(x) returns None and modifies the list in-place by removing x it exists inside the list. When you do

a = a.remove(word)

you will be effectively storing None in a and this would give an exception in the next iteration when you again do a.remove(word) (None.remove(word) is invalid), but you don’t get that either since you immediately return after the conditional (which is wrong, you need to return after the loop has finished, outside its scope). This is how your function should look like (without modifying a list while iterating over it):

remove_words = ["abc", ...] # your list of words to be removed

def remove_make(x):
    a = x.split()
    temp = a[:]
    for word in temp: 
        if word in remove_words: # True
            a.remove(word)
    # no need of 'else' also, 'return' outside the loop's scope
    return " ".join(a)
Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • Be careful with variable `remove`, because it is not a parameter of the function and it is not definied init as well. And the split can make some trouble, too. If you split "I like you." you get `a=['I', 'like', 'you.']` If you want to remove "you", this will fail. – mosc9575 Jan 17 '21 at 09:27
  • Nothing is mentioned regarding the punctuation, I don’t even know how the string is supposed to be created back from the list (they don’t even return a string but the list itself btw), this answer only aims towards fixing the current issue with it, not regarding how to create a correct, sane list back from the split list. If I were to remove the words, I would either use regex or list comprehension instead, which I could’ve mentioned in the answer, but again, that’s not the real issue here. I think question needs more details in that aspect. – Jarvis Jan 17 '21 at 09:31
  • 1
    Your are right. But I think it is important to mention that punctuation will have an influence to the result. – mosc9575 Jan 17 '21 at 09:33
0

You can create a new list without the words you want to remove and then use join() function to concatenate all the words in that list. Try

def remove_words(string, rmlist):
    final_list = []
    word_list = string.split()
    for word in word_list:
        if word not in rmlist:
            final_list.append(word)
    
    return ' '.join(final_list)
Janska
  • 41
  • 3