vendredi 22 janvier 2016

Iterate through list, print iterable if the condition is met

I have a list, containing random items, some of them consists of both numbers and letters.

my_list = ["a a1", "b 2b", "c c", "d d"]

I need to print items that are only consisting of letters. I tried

for i in my_list: for j in i.split(): if j.isalpha(): print j

but it returns only

c c
d d 

I also tried

for i in my_list: if any(n.isalpha() for n in i.split()): print n

but it returns

NameError: name 'i' is not defined

Aucun commentaire:

Enregistrer un commentaire