EmJ
EmJ

Reputation: 4608

How to efficiently remove single letter words in Python

I want to remove single letter words such as a, i e, e g, b f f, y o l o, c y l using a Python function.

My current code looks follows.

def remove_single_letters(concept):
    mystring = "valid"

    if len(concept) == 1:
        mystring = "invalid"

    if len(concept)>1:
        validation = []
        splits = concept.split()
        for item in splits:
            if len(item) > 1:
                validation.append("valid")
        if len(validation) != 1:
            mystring = "invalid"
    return mystring

print(remove_single_letters("b f f"))

It works fine. However, I am wondering if there is a more efficient way (with lesser time) of doing it in python.

Upvotes: 3

Views: 1714

Answers (2)

Farzad Vertigo
Farzad Vertigo

Reputation: 2828

I would go for a more concise solution (yet not faster as both solutions are of O(n)) if you want to check if any 1 letter character exists in the string:

remove_single_letters = lambda concept:"invalid" if 1 in [len(item) for item in concept.split()] else "valid"
print(remove_single_letters("a b c"))
#prints invalid

An ordinary function would be like this:

def remove_single_letters(concept):
    return "invalid" if 1 in [len(item) for item in concept.split()] else "valid"

They both check the length of elements in the split input to find any item with length of 1 and is insensitive to multiple spaces thanks to split().

If you want to check the strings that are totally made up of single characters:

def remove_single_letters(concept):
    u = set(len(item) for item in concept.split()) 
    return "invalid" if len(u) == 1 and 1 in u else "valid"

Upvotes: 1

Selcuk
Selcuk

Reputation: 59229

Here is a single line solution:

def remove_single_letters(concept):
    return ["valid", "invalid"][concept.count(" ") >= len(concept) // 2]

Update: Note that this is shorter and cool looking but does not necessarily run faster.

Explanation:

  • concept.count(" "): Returns the number of spaces in string
  • >= len(concept) // 2: Returns True if more than half of the string is spaces (it fails when there are multiple spaces between legitimate words as @user202729 mentioned)
  • ["valid", "invalid"][result]: This part is just for fun: It returns the first element if the result is False and the second element if the result is True (because False is equal to 0 and True is equal to 1).

Upvotes: 6

Related Questions