[python] How to count the number of letters in a string without the spaces?

For another one-liner solution:

def count_letters(word):  return len(filter(lambda x: x not in " ", word))

This works by using the filter function, which lets you pick the elements of a list that return true when passed to a boolean-valued function that you pass as the first argument. I'm using a lambda function to make a quick, throwaway function for that purpose.

>>> count_letters("This is a test")
11

You could easily extend this to exclude any selection of characters you like:

def count_letters(word, exclude):  return len(filter(lambda x: x not in exclude, word))

>>> count_letters ("This is a test", "aeiou ")
7

Edit: However, you wanted to get your own code to work, so here are some thoughts. The first problem is that you weren't setting a list for the Counter object to count. However, since you're looking for the total number of letters, you need to join the words back together again rather than counting each word individually. Looping to add up the number of each letter isn't really necessary because you can pull out the list of values and use "sum" to add them.

Here's a version that's as close to your code as I could make it, without the loop:

from collections import Counter
import string

def count_letters(word):
   wordsList = string.split(word)
   count = Counter("".join(wordsList))
   return sum(dict(count).values())

word = "The grey old fox is an idiot"
print count_letters(word)

Edit: In response to a comment asking why not to use a for loop, it's because it's not necessary, and in many cases using the many implicit ways to perform repetitive tasks in Python can be faster, simpler to read, and more memory-efficient.

For example, I could have written

joined_words = []
for curr_word in wordsList:
    joined_words.extend(curr_word)
count = Counter(joined_words)

but in doing this I wind up allocating an extra array and executing a loop through the Python interpreter that my solution:

count = Counter("".join(wordsList))

would execute in a chunk of optimized, compiled C code. My solution isn't the only way to simplify that loop, but it's one way.