Table of Contents
Use for
loop with replace()
Method
To remove punctuation from list in Python:
- Use the
for
loop to iterate over each word (element) of given list. - Use a nested loop to iterate over each character in the word.
- Use the
if
statement to check if the current character is instring.punctuation
, if it is usereplace()
to replace punctuation marks with an empty string. - After iterating over the each word, use
append()
to append theword
into thenew_list
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import string my_list = ["How's", "Hello,", "Goodbye!", "Take Care", ""] new_list = [] for word in my_list: for character in word: if character in string.punctuation: word = word.replace(character,"") new_list.append(word) print(new_list) |
We will get the below output on the successful execution of the above program.
1 2 3 |
['Hows', 'Hello', 'Goodbye', 'Take Care', ''] |
We used a for
loop to iterate over all the elements of the list.
The nested for
loop is used to iterate over each word character by character.
We checked if the character is in string.puntucation
, if it is so then, used the replace()
method to replace the punctuation character with an empty string.
In Python, the string.puncutation
is the pre-initialized string which has all the punctuation marks, we can also use print them as print(string.punctuation)
to have a look at all the punctuation marks.
The replace()
method takes two phrases where the first pattern/phrase is replaced with the second pattern/phrase.
By default, the replace()
function replaces all the occurrences but we can also pass a count
to remove punctuation from the first n
elements of a list.
Once we have iterated over the word
, use the append()
method to append word
to the my_list
.
Here, we used the append()
method to append elements to the end of the list, which is new_list
in our case. Finally, we used a print
statement to print the updated list.
You may have noticed that we had an empty string in the above output that is not required. For that, we can add another condition to assess if the current element is an empty string or not.
If it is so, we can remove that element from the list; otherwise jump to the else
part.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import string my_list = ["How's", "Hello,", "Goodbye!", "Take Care", ""] new_list = [] for word in my_list: if word =="": my_list.remove(word) else: for character in word: if character in string.punctuation: word = word.replace(character,"") new_list.append(word) print(new_list) |
Now, we will get the following outcome.
1 2 3 |
['Hows', 'Hello', 'Goodbye', 'Take Care'] |
This code is similar to the first code example but we used the if-else
statement to check if the word
is an empty string or not.
If it is then, we used the remove()
function to eliminate the specified word
from the list; otherwise, moved ahead to the else
section.
Use Nested List Comprehension
To remove punctuation from the given list in Python:
- Use an inner
for
loop to iterate over each word character by character and check if that character belongs tostring.punctuation
.
Usestr.join()
to join all the characters returned by the above loop with the empty string and produce a whole word which does not has any punctuation signs. - Use an outer
for
loop to execute the inner list comprehension for every word inside themy_list
list. - Store all the words returned by the outer list comprehension into a
new_list
list.
1 2 3 4 5 6 7 8 9 |
import string my_list = ["How's", "Hello,", "Goodbye!", "Take Care", ""] new_list = [''.join(character for character in word if character not in string.punctuation) for word in my_list] print(new_list) |
We will get the following list after running the above program.
1 2 3 |
['Hows', 'Hello', 'Goodbye', 'Take Care', ''] |
Usually, this code seems difficult to understand to most people but let’s break it down into chunks to understand.
The above code is using nested list comprehension which has two sections, the inner and outer parts.
The ''.join(character for character in word if character not in string.punctuation)
is the inner part while for word in my_list
is the outer part of list comprehension. Here, the outer part runs the inner part as follows.
In the inner part, we used a for
loop to iterate over each word
letter by letter and make sure that the character
is not in string.punctuation
.
The str.join()
wrapping the inner part joined all the returned characters with the empty string.
In the outer part, we used a for
loop to execute the inner part for every word
in my_list
and save the returned words (without punctuation signs) in new_list
.
Finally, we used the print
statement to display the results on the console.
This approach is preferable where we’ve limited memory space because list comprehension not only serves the same results with fewer lines of code but lets us perform
inplace
modifications. It means, we don’t have to create new lists but we can update the original list.
Notice that we are again having an empty string in the above output that we can remove by using an additional if
statement in the outer part of the list comprehension as follows.
1 2 3 4 5 6 7 8 9 10 |
import string my_list = ["How's", "Hello,", "Goodbye!", "Take Care", ""] new_list = [''.join(character for character in word if character not in string.punctuation) for word in my_list if word] print(new_list) |
The above program will show the following output.
1 2 3 |
['Hows', 'Hello', 'Good bye', 'Take Care'] |
Further reading:
Use str.translate()
Function
To eliminate punctuation signs from the given list in Python:
- Use the
str.translate()
function with pre-initializedstring.punctuation
constant and list comprehension.
1 2 3 4 5 6 |
import string my_list = ["How's", "Hello,", "Goodbye!", "Take Care", ""] new_list = [word.translate(string.punctuation) for word in my_list] print(new_list) |
We will get the following results after running the above code fence successfully.
1 2 3 |
["How's", 'Hello,', 'Goodbye!', 'Take Care', ''] |
Using str.translate()
is much easier to understand and faster in terms of execution as compared to the last two approaches.
The list comprehension executed the word.translate(string.punctuation)
for each word
in my_list
while the str.translate(string.punctuation)
mapped each character in string.punctuation
to an empty string.
Note that, the str.translate(string.punctuation)
mapped each character for every word
in my_list
.
Finally, we saved all the returned elements by list comprehension in new_list
and display them on the screen.
We can also add an if
statement to remove the empty string from the output.
1 2 3 4 5 6 |
import string my_list = ["How's", "Hello,", "Goodbye!", "Take Care", ""] new_list = [word.translate(string.punctuation) for word in my_list if word] print(new_list) |
1 2 3 |
["How's", 'Hello,', 'Goodbye!', 'Take Care'] |
That’s all about how to remove punctuation from List in Python.