By Aarav Chandra


2019-04-15 11:00:04 8 Comments

I have two inputs (Text - a string, L1- List of strings to be excluded).

I have converted the 'Text' into a list and extracted each word and put it into a list using the following code:

Text=list(Text.split())

Now I have to remove the words present in the L1 list from this 'Text' list. To do so, I used the following code:

for x in Text:
        if(x in L1):
            Text.remove(x)
print(Text)

Inputs:

Text = "jack and jill went to the market to buy bread and cheese cheese is jack favorite food"

L1 = ["and","he","the","to","is"]

Desired Output:

['jack', 'jill', 'went', 'market', 'buy', 'bread', 'cheese', 'cheese', 'jack', 'favorite', 'food']

Actual Output:

['jack', 'jill', 'went', 'the', 'market', 'buy', 'bread', 'cheese', 'cheese', 'jack', 'favorite', 'food']

Please tell me why is 'the' still present in the 'Text' ?

What did I do wrong? What should I do to get my desired result?

5 comments

@Yash Shukla 2019-04-16 12:20:26

Split_text= Text.split() matched= [x for x in Split_text if x not in L1] print(matched)

@Sreeram TP 2019-04-15 11:02:49

You can simply use a list comprehension like this to get desired output

Text = "jack and jill went to the market to buy bread and cheese cheese is jack favorite food"

L1 = ["and","he","the","to","is"]

Text= Text.split()

removed = [x for x in Text if x not in L1]

print(removed)

# Output : ['jack', 'jill', 'went', 'market', 'buy', 'bread', 'cheese', 'cheese', 'jack', 'favorite', 'food']

The reason your code is not working as intended is you are iterating over the list and at the same time you are altering it, which is something that should not be done.

As @blubberdiblub mentioned in the comments, this code has a time complexity of O(n*m). This can be improved to O(n+m) if we can make sure that there is no repetition in the list L1. For that use set representation of L1.

@blubberdiblub 2019-04-15 11:13:21

Note that for large numbers of items in both x and L1 (speaking of - say - thousands of items or so) it can be better performance-wise to get a set() representation of L1 before repeatedly doing in checks on it. That will reduce time complexity from O( n * m ) to O( n + m ).

@MusicForLife 2019-04-15 11:13:57

The reason that your code is not working is that you are iterating over the list and at the same time making changes in the list.

@Hoog 2019-04-15 11:09:55

You should not mess with a list while you are iterating over that list. In here:

for x in Text:
        if(x in L1):
            Text.remove(x)
print(Text)

When you remove x from your list your for loop then tries to find the next element in Text to loop over, but one was just pulled out from under it so it ends up going one too far, and not looping as you would like. As mentioned in another post you can use a list comprehension or you could save the spots to remove for later removal:

toRemove = []
for x in Text:
        if(x in L1):
            toRemove.append(x)

for x in toRemove:
    Text.remove(x)
print(Text)

But the list comprehension way is much nicer

@brunns 2019-04-15 11:04:35

The reason that this isn't working is that you're modifying the list as you're iterating over it, which doesn't work, as you see. One option would be to iterate over a copy of the list, but Sreeram TP's answer is the best approach I think.

Related Questions

Sponsored Content

43 Answered Questions

[SOLVED] How can I represent an 'Enum' in Python?

27 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3119813 View
  • 3413 Score
  • 27 Answer
  • Tags:   python directory

35 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2159288 View
  • 3192 Score
  • 35 Answer
  • Tags:   python list

18 Answered Questions

[SOLVED] How can I make a time delay in Python?

17 Answered Questions

[SOLVED] How to make a chain of function decorators?

44 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

42 Answered Questions

[SOLVED] How do I sort a dictionary by value?

42 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

9 Answered Questions

[SOLVED] How can I explicitly free memory in Python?

Sponsored Content