mercredi 28 février 2018

If statement that keeps characters only

Let's say we have a list list_a = [a,b,C,.,/,!,d,E,f,]

i want to append to a new list only the letters of the alphabet.

So the new list will be list_b = [a,b,C,d,E,f]. So far i have tried doing it like that way:

list_b = []
for elements in list_a:
    try:
        if elements == str(elements):
            list_b.append(elements)
    except ValueError: #Catches the Error when the element is not a letter
        continue

However, when i print the list_b it has all the elements of list_a , it doesn't do the job i expected. Any ideas ?

PS: comma in the specific example brings Error too.

Aucun commentaire:

Enregistrer un commentaire