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?


import_contacts

You may also like:

Related Posts

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63dc60945ab03164849959/] [crayon-63dc60945ab09750113800/] First, we created a […]

  • 04 October

    Get Every Other Element in List in Python

    Table of ContentsGet Every Other Element in List in PythonUsing List SlicingUsing the list.append() function along with the range() functionUsing list comprehension and the range() function with conditionalsUsing list comprehension and the range() function with stepUsing the enumerate() function along with list comprehensionUsing a lambda functionConclusion. A list is one of the more versatile data […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.Conclusion In this post, we will […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusion In this post, we will see how to compare list elements with each other in Python. Compare all […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.