Random Word Selection by Length Range

  • Share this:

Code introduction


This function takes a list of words and returns a random word with length between the specified minimum and maximum length.


Technology Stack : Textual, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_word_length(word_list, min_length, max_length):
    """
    This function takes a list of words and returns a random word with length between min_length and max_length.
    """
    import random
    from textual import random

    def get_word_length(word):
        return len(word)

    filtered_words = filter(lambda word: min_length <= get_word_length(word) <= max_length, word_list)
    return random.choice(list(filtered_words))

# JSON representation                
              
Tags: