Remove specific string and all number and blank string from array in python

Question:

I want to remove some words and all string that contains number from this array and

reviews = ['', 'alternative', ' ', 'transcript', ' ', 'alive I', 'm Alive I', 'm Alive I', 'm Alive', ' ', 'confidence', ' 0', '987629', ' ', 'transcript', ' ', 'alive I', 'm Alive I', 'm Alive I', 'm Alive yeah', ' ', 'transcript', ' ', 'alive I', 'm Alive I', 'm Alive I', 'm Alive life', ' ', 'final', ' True', '']

for review in reviews:
    if review == '' or review == ' ' or review == 'alternative' or review == 'transcript' or review.replace(' ', '').isdigit() or review.isdigit():
        reviews.remove(review)

print(reviews)

but I am not getting expected output from the array, it’s output showing number and the word as well.
[‘alternative’, ‘transcript’, ‘alive I’, ‘m Alive I’, ‘m Alive I’, ‘m Alive’, ‘confidence’, ‘987629’, ‘transcript’, ‘alive I’, ‘m Alive I’, ‘m Alive I’, ‘m Alive yeah’, ‘transcript’, ‘alive I’, ‘m Alive I’, ‘m Alive I’, ‘m Alive life’, ‘final’, ‘ True’]

I cannot not understand what I am doing wrong. Please help.

Asked By: Ferdousi

||

Answers:

You are trying to remove certain words and strings that contain numbers from a list called reviews.

You are modifying the list that you are iterating over (reviews) by calling reviews.remove(review) inside the loop. This can cause problems because the list size is changing as you iterate over it, which can result in unexpected behavior and may even cause the loop to skip some elements.

To fix this, you can use a different approach such as creating a new list that only contains the elements you want to keep. Here is an example of how you can do this:

filtered_reviews = []
for review in reviews:
    if review and review != ' ' and review != 'alternative' and review != 'transcript' and not review.replace(' ', '').isdigit() and not review.isdigit():
        filtered_reviews.append(review)

print(filtered_reviews)
Answered By: blessedpulpo

The problem is that when you remove an element from list in a for loop, the iterator will not work as you want.

If you want to remove elements inplace, you can use while maybe:

i = 0
while i < len(reviews):
    review = reviews[i]
    if review == '' or review == ' ' or review == 'alternative' or review == 'transcript' or review.replace(' ', '').isdigit() or review.isdigit():
        reviews.remove(review)
    else:
        i += 1
Answered By: Amin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.