Created
May 2, 2017 00:06
-
-
Save sethbunke/8cde5b692f9d18a2dede1e5b37b56304 to your computer and use it in GitHub Desktop.
Filter strings of a minimum and maximum length using list comprehension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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