Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 26, 2018 23:06
Show Gist options
  • Save bbookman/e5c6030a9e2624bbdb85e1e5e2efa37f to your computer and use it in GitHub Desktop.
Save bbookman/e5c6030a9e2624bbdb85e1e5e2efa37f to your computer and use it in GitHub Desktop.
Python List Comprehension: Find words with more than 4 letters
'''Find all of the words in a string that are less than 4 letters'''
sentence = 'On a summer day somner smith went simming in the sun and his red skin stung'
examine = sentence.split()
result = [word for word in examine if len(word) >=4]
print(result)
@kaithecode
Copy link

sentence = 'On a summer day somner smith went simming in the sun and his red skin stung'
words=tuple(sentence.split(' '))

result =[i for i in words if len(i) < 4]
print (result)

I think mine should be shorter the code?

@Amine-Fadssi
Copy link

# Find all the words in a string that are less than 4 letters
string = 'all the words in a string'
my_list = [word for word in string.split(' ') if len(word) < 4]
print(my_list)

@Folskyy
Copy link

Folskyy commented Jun 15, 2024

"""
Find all of the words in a string that are less than 4 letters
"""
words = [i for i in string.split() if len(i) < 4]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment