Rajeev
Rajeev

Reputation: 46909

Set list to None in list comprehension

In the following code any element after splitting does end with 'div' then it should be the resultant of new list but right now i get b = [''] .My question is how to make the list empty b=[]

a=['1div,2div,3div,4div,5div']

b= [','.join(i for i in a[0].split(',') if not a.endswith('div'))]

Upvotes: 0

Views: 1391

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250931

After fixing the errors in your code, you can do:

result = [','.join(i for i in a[0].split(',') if not i.endswith('div'))]
b = result if result[0] else []
#or
result = ','.join(i for i in a[0].split(',') if not i.endswith('div'))
b = [result] if result else []

And using just a one element list makes no sense here.

Upvotes: 1

idanshmu
idanshmu

Reputation: 5251

what about:

filter(lambda x: not x.endswith('div'), a[0].split(","))

Upvotes: 0

Related Questions