Complete the code to convert text into a list of words.
text = "Hello world" words = text.[1]()
The split() method breaks a string into a list of words based on spaces.
Complete the code to convert words into their numerical indices using a dictionary.
word_to_index = {'hello': 1, 'world': 2}
indices = [word_to_index[[1]] for word in ['hello', 'world']]We use the variable word to look up each word's index in the dictionary.
Fix the error in the code to convert text to lowercase before tokenizing.
text = "Hello World" tokens = text.[1]().split()
Using lower() converts all letters to lowercase, which helps standardize text before processing.
Fill both blanks to create a dictionary mapping words to their lengths for words longer than 3 letters.
words = ['apple', 'cat', 'banana', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension maps each word to its length using len(word). The condition len(word) > 3 filters words longer than 3 letters.
Fill all three blanks to create a dictionary of uppercase words mapped to their lengths for words shorter than 6 letters.
words = ['apple', 'cat', 'banana', 'dog'] result = { [1]: [2] for w in words if len(w) [3] 6 }
The dictionary comprehension uses w.upper() as keys and len(w) as values. The condition len(w) < 6 filters words shorter than 6 letters.