0

How do you remove punctuation from a list using a for loop? I have imported a string of punctuation and I am using this to compare to the original list in order to remove the punctuation.

This is my current code:

import string

l = list(string.punctuation)
print(punctuation_list)

w = ["haythem", "is", "eating", "tacos.", "haythem", "loves", "tacos", "", ":"]

w_clean = list()
                  
for x in w:
    for y in l:
        if y in x:
            x = x.replace(y,'')
            w_clean.append(x)
            break

print(w_clean)

And the output is:

['!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']

['tacos', '']

The required output is:

['haythem', 'is', 'eating', 'tacos', 'haythem', 'loves', 'tacos']
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Aidan
  • 3
  • 2
  • 1
    Please update your question with the required output. – quamrana Jul 14 '21 at 11:14
  • 2
    Your code is working as intended. It returns any string which has punctuation, with that punctuation removed. In the example code, that is `'tacos.'` & `':'`. – SiHa Jul 14 '21 at 11:14
  • 2
    By the way - single character variables make code *very* hard to debug. – SiHa Jul 14 '21 at 11:15
  • That's the required output on it now, thanks for pointing that out! – Aidan Jul 14 '21 at 14:51
  • Does this answer your question? [Best way to strip punctuation from a string](https://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string) – Tomerikoo Jul 14 '21 at 18:23

3 Answers3

1

You are currently only appending strings if punctuation was found.

You can either remove the if clause or you cann add an else that also appends the other strings.

Also you should remove the break block, unless you only want to remove the first punctuation.

So the inner block should look like this:

if y in x:
    x = x.replace(y,'')
    w_clean.append(x)
else:
    w_clean.append(x)

Also check out this answer for more efficient replacement methods.

Ant
  • 151
  • 5
  • Hey! Thanks a lot for the help, I tried this method but this seems to output a really long answer in which all the words are repeated multiple times. Do you know why this may be? – Aidan Jul 14 '21 at 14:42
  • It looks like the `w_clean.append(x)` is executed for each punctuation character examined. – quamrana Jul 14 '21 at 15:01
1

In your code in the inner loop you're checking whether the punctuation is in the word. If so, replace the punctuation with empty string and add to the output list. So, you're code is working accordingly what you've written.

But I assume what you want is to return the input list of words with no punctuations in it. I also assumed you don't need the empty words in your list and you allow duplicates (like tacos) So for this, you can modify your code like this:

for x in w:
    for y in punctuation_list:
        if y in x:
            x = x.replace(y,'')
if(len(x)>0):
    w_clean.append(x)    

if you want to keep the empty strings, just remove the if condition and add w_clean.append(x) under the first loop.

devReddit
  • 2,696
  • 1
  • 5
  • 20
  • Hey, thanks for the help and the quick response! I tried this but it didn't seem to work. This is what I have just now: w_clean = list() for x in w: for y in l: if y in x: x = x.replace(y, "") w_clean.append(x) if(len(x)>0): w_clean.append(x) print(w_clean) break. This then outputs the list on different lines, each time adding another word, as expected since it is a loop. However the punctuation didn't get removed – Aidan Jul 14 '21 at 14:47
  • It seems that the code is not computing the first if, as when I put a print underneath it to see what it is outputting, nothing shows up. – Aidan Jul 14 '21 at 14:50
  • I guess there's wrong indentations in your code, please check again, the solution gives your expected result – devReddit Jul 14 '21 at 14:52
  • I've added an image to my answer, please crosscheck the indentations. – devReddit Jul 14 '21 at 14:56
  • Please avoid posting images (or worse, links to images) of code or errors. Anything text-based (code and errors) should be posted as text directly in the question itself and formatted properly as a [mre]. You can get more [formatting help here](https://stackoverflow.com/help/formatting). You can also read about [why you shouldn't post images/links of code](//meta.stackoverflow.com/q/285551). – Tomerikoo Jul 14 '21 at 15:11
  • removed the image – devReddit Jul 14 '21 at 15:15
1

Following on the answer by Ant:

import string
l = list(string.punctuation)

w = ["haythem", "is", "eating", "tacos.", "haythem", "loves", "tacos", "", ":"]

w_clean = []
                  
for x in w:
    for y in l:
        if y in x:
            x = x.replace(y,'')
    if x:
        w_clean.append(x)
print(w_clean)

Output as requested

quamrana
  • 37,849
  • 12
  • 53
  • 71