How to return all list elements of a given length?

I would use a list comprehension:

def by_size(words, size):
    return [word for word in words if len(word) == size]

Leave a Comment