0

I was trying to figure out how to list just the letters in a string and ignore the numbers or any other characters. I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop. This is how I used the for loop:

>>> a = "Today is April 1, 2016"
    for i in a:
         if i.isalpha():
              list(i)

Any help will be appreciated!

glibdud
  • 7,550
  • 4
  • 27
  • 37
B.D
  • 7
  • 3

2 Answers2

1

You can use filter for this:

>>> ''.join(filter(str.isalpha, a))
'TodayisApril'
arekolek
  • 9,128
  • 3
  • 58
  • 79
0
list(set([x for x in a if x.isalpha()]))

this should do it :)

Daniel Severo
  • 1,768
  • 2
  • 15
  • 22
  • From the question: `I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop.` and this answer uses `for` loop. – Lafexlos Apr 01 '16 at 17:54
  • It's a list comprehension actually ... http://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops – Daniel Severo Apr 01 '16 at 17:56
  • Thank you for all of the answers! I ended up finding filter for this. – B.D Apr 01 '16 at 19:51