a function to remove a none value from a stripping code

Viewed 46
final_company_name = [clean.strip('\n') for clean in clean_company_name]

this is my code that is trying to strip from the information am getting but am having no value among it and have been getting this error, AttributeError: 'NoneType' object has no attribute 'strip'

2 Answers

This should do the job. Just add if clean at the end of your list comprehension.

final_company_name = [clean.strip("\n") for clean in clean_company_name if clean]

Edit: Fails if clean is of type bool. Use if clean and isinstance(clean, str) like Sharim Iqbal mentioned.

The @js-on answer only works, But failed, If there is a True keyword or any int or float type element inside your list, Like None is in your list.

final_company_name = [clean.strip("\n") for clean in clean_company_name if clean and isinstance(clean,str)]
Related