Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sethbunke/8cde5b692f9d18a2dede1e5b37b56304 to your computer and use it in GitHub Desktop.
Save sethbunke/8cde5b692f9d18a2dede1e5b37b56304 to your computer and use it in GitHub Desktop.
Filter strings of a minimum and maximum length using list comprehension
min_length = 1 #as long or longer than this
max_length = 4 #no longer than this
data = ['', 'a', 'bb', 'cc', '', 'ddd', 'eee', 'ffff', 'ggggg', 'hhhhhh']
results = [item[:max_length] for item in data if len(item) >= min_length]
print(results)
#['a', 'bb', 'cc', 'ddd', 'eee', 'ffff', 'gggg', 'hhhh']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment