How to return elements from a list that have a certain length?

后端 未结 5 918
一生所求
一生所求 2020-11-30 14:14

I\'m trying to return words that have a specific length size.

The words is a list and size is a positive integer here. The result should be

5条回答
  •  星月不相逢
    2020-11-30 14:35

    I would use a list comprehension:

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

提交回复
热议问题