-4

I've a list comprising of numbers and names.

lst = ['new car', '232', 'famous bike','232', 'new car', '232plane', 'new car', 'plane232']

I want to count only the words like new car, famous bike and not numeric or alphanumeric.

Output will be 2 because there are two unique words: car, bike.

I know there's a simple answer to it but I can't get my head on to it.

Thanks.

YOLO
  • 20,181
  • 5
  • 20
  • 40
  • Not sure what you mean by "pure strings" - why wouldn't "232" be a pure string? Also: Please edit your question to show what you've tried and where you're stuck. – David Makogon Apr 29 '18 at 16:43
  • 1
    You tried *nothing*? Even not a naïve – but working – loop? – Jongware Apr 29 '18 at 16:44
  • Sorry, my bad. I couldn't land on these duplicate questions earlier. Thanks. @usr2564301 – YOLO Apr 29 '18 at 16:46

1 Answers1

5

You can use str.isalpha to determine if a string contains only letters. Then make a set from that (to make a container of unique elements) then determine the length of that set

>>> len(set(i for i in lst if i.isalpha()))
2
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • thank you cory :) but i've updated the list with a new input. Your solution doesn't solve that case. @CoryKramer – YOLO Apr 29 '18 at 16:47
  • 1
    @YOLO For your updated list, you could try: `len(set(x for x in lst if x.replace(' ', '').isalpha()))`. – Austin Apr 29 '18 at 17:08