Remove Punctuation from List in Python

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 in string.punctuation, if it is use replace() to replace punctuation marks with an empty string.
  • After iterating over the each word, use append() to append the word into the new_list.

We will get the below output on the successful execution of the above program.

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.

Now, we will get the following outcome.

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 to string.punctuation.
    Use str.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 the my_list list.
  • Store all the words returned by the outer list comprehension into a new_list list.

We will get the following list after running the above program.

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.

The above program will show the following output.

Use str.translate() Function

To eliminate punctuation signs from the given list in Python:

  • Use the str.translate() function with pre-initialized string.punctuation constant and list comprehension.

We will get the following results after running the above code fence successfully.

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.

That’s all about how to remove punctuation from List in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *