How to change a list using list comprehension with if and else in Python

1 Answer

0 votes
lst = [' python', 'c', '  c++', None, 'javascript', '    java', "c#", "swift", None]

lst = [str(x.strip()) if x is not None else '' for x in lst]

print(lst)



'''
run:

['python', 'c', 'c++', '', 'javascript', 'java', 'c#', 'swift', '']

'''

 



answered Apr 19, 2021 by avibootz
...