1

I have a python array which contains multiple arrays with sentences, each sentence having a certain amount of elements (words).

Example:

[
    ['im', '23/f', '.'],
    ['want', 'back', '.'],
    ['one', 'got', 'altogether', '.'],
    ['used', 'multitask', 'before', '.'],
    ['excellent', '.'],
    ['people', 'pleaser', ',', 'really', 'see', 'expectations', '.'],
    ['also', 'perfect', 'daughter', 'good', 'grades', '.'],
    ['responsible', '.'],
    ['enthusiastic', 'dreamed', 'big', '.'],
    ['im', '..', 'fat', 'tired', 'time', '.'],
    ['find', 'everything', 'tiring', '.'],
    ['studying', 'seems', 'pointless', '.'],
    ['want', 'something', 'new', 'time', ',', 'anxiety', '.'],
    ['wan', 'na', 'brave', 'independent', 'time', ',', 'find', 'comfort', 'depending', 'people', '.'],
    ['know', ',', 'suck', '.'],
    ['stuck', '.'],
    ['know', 'story', 'leads', '.'],
    ['know', 'me', '.'],
    ['seems', 'like', 'dont', 'life', 'eyes', 'anymore', '.']
]

I want to remove all the characters like "full stops" or "question marks" etc.

I wrote this simple for loop to do so, but it only removes most, but not all "full stops".

for sentence in body:
    for word in sentence:
        if len(word) < 3:
            sentence.remove(word)

How can it not remove all words that have fewer than 3 characters but only some?

imxitiz
  • 3,920
  • 3
  • 9
  • 33
be2021
  • 29
  • 6

2 Answers2

1

It's because when removing elements from a list, the index kept for iterating over the list doesn't change on a remove. For this reason if there are two words with length of less than three, the second one will be skipped.

Let's say we have ['aaaa', 'bbb', 'ccc', 'wwww', 'yyyyy']. You go over 'aaaa', the index is 0. You go to 'bbb', index 1. You delete 'bbb' and iterator will go on index = 2. However because now you have ['aaaa', 'ccc', 'wwww', 'yyyyy'], it will go to 'wwww' and not 'ccc'.

1

To took all elements in list, you have to add all index [:] . just use:

for sentence in body:
    for word in sentence[:]:
        if len(word) < 3:
            sentence.remove(word)
Berlin Benilo
  • 472
  • 1
  • 12