Need to remove the punctuation marks from the list and then save it in the same list
Code :
sentences = ["Hi! How are you doing?","Hope everything is fine.","Have an amazing day!"]
type(list1)
sentences = sentences.translate(str.maketrans('', '', string.punctuation))
Error :
AttributeError Traceback (most recent call last)
<ipython-input-4-7b3b0cbf9c58> in <module>()
1 sentences = ["Hi! How are you doing?","Hope everything is fine.","Have an amazing day!"]
2 type(list1)
----> 3 sentences = sentences.translate(str.maketrans('', '', string.punctuation))
AttributeError: 'list' object has no attribute 'translate'
The error is telling you the truth. A list does not have a translate
attribute — a string does. You need to call this on the strings in the list, not the list itself. A list comprehension is good for that. Here you can call translate()
on each string in the list:
import string
sentences = ["Hi! How are you doing?","Hope everything is fine.","Have an amazing day!"]
trans = str.maketrans('', '', string.punctuation)
[s.translate(trans) for s in sentences]
# ['Hi How are you doing', 'Hope everything is fine', 'Have an amazing day']