SS123 SS123 - 26 days ago 6
Python Question

Python filter() function and list comparison

I have a list in python like

l=('A','1,''B','2','C','3,''D','4')
and i need to filter out the value of A,B,C,D so i wrote a code like follow

list(filter(lambda x:x.isalpha(),l))


then returned
['A', 'C']


it did't return value B and D so i thought it was some thing i don't know about python filter function then i wrote it in list comparison like follows

[i for i in l if i.isalpha()]


but the strange thing is it also return the
['A', 'C']
so every time what happen to the value B and D

Any one can explain me how to filter all alphabetic values out ???

Answer

You're malforming the list, it should be:

l=('A','1','B','2','C','3','D','4')

Notice the commas

Hope it helps!