I need to remove some specific character from a list of element, but some element is None.
I have a list of elements, such as
row= ['Hello,World','$Hello World', None]
I need a python code to remove',' and '$'from the element. My code looks like this but is an invalid syntax:
row=[item.replace('$','').replace(',','') if item is not None for item in row]
I need to keep the None in the list, so the required output should be ['HelloWorld', 'Hello World', None]
How to fix it?
[item.replace('$', '').replace(',','') for item in row if item]
If you want to keep the None
[item.replace('$', '').replace(',','') if item else item for item in row]
Note: If you have anything other than strings and Nonetypes in the list, it'd be a good idea to check the element is a string or responds to the replace method before calling replace.